Here's a list of some of the most commonly used Linux commands along with a brief one-liner definition for each:
ls
- Lists the contents of a directory.cd
- Changes the current directory to another directory.pwd
- Prints the current working directory path.cp
- Copies files or directories from one location to another.mv
- Moves or renames files or directories.rm
- Removes (deletes) files or directories.mkdir
- Creates a new directory.rmdir
- Removes an empty directory.touch
- Creates an empty file or updates the timestamps of an existing file.echo
- Displays a line of text/string that are passed as an argument.grep
- Searches for patterns in files and outputs the matching lines.find
- Searches for files and directories based on criteria like name, size, and modification time.cat
- Concatenates and displays the content of files.head
- Outputs the first part of files, typically used to display the start of a file.df
- Displays disk space usage for file systems.du
- Estimates and displays the disk space used by files and directories.ps
- Displays information about a selection of the active processes.kill
- Sends a signal to a process, typically used to end a process.tar
- Creates, extracts, or lists files from a tape archive file.wget
- Non-interactive network downloader for downloading files from the web.curl
- Tool to transfer data from or to a server, supporting various protocols.
To view what's written in a file
cat-
is used. The basic syntax is:
Here, replace the filename with actual name of the file you want to view.
To check which commands you have run till now
history --
This is used to check comaands run till now.
To remove a Directory/Folder.
Depending on whether the directory is empty or contains files. Here's how to use commands:
a) Removing an Empty Directory: When the directory is empty, you can use the rmdir
command.
Replace directory_name
with the name of the directory you want to remove.
b) Removing a Non-Empty Directory: You need to use the rm
command with the -r
(or --recursive
) option to remove the directory and its contents recursively:
Note- when using rm -r
as it will permanently delete the directory and all its contents, so be careful.
To change the access permissions of files.
The chmod
command to change the access permissions of files. This command allows you to modify the read, write, and execute permissions for the owner, group, and others. Below is the basic syntax:
Replace permissions
with the desired permission settings, and filename
with the name of the file you want to modify.
For example, "rwxr-x---" means:
The owner can read, write, and execute the file.
The group can read and execute the file.
Others have no permissions at all.
Lets now observe few tasks:
To create a colours.txt file and to view the content.
1.Creating the colours.txt
file:
- Viewing the content of
colours.txt
:
Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.
use the echo
command along with the >
operator to add content to the Colors.txt
file one in each line.
Note--e
option enables the interpretation of escape sequences, allowing you to use \n
for newlines.
>
: This operator redirects the output of theecho
command to a file.
Add content in devops.txt (One in each line)--A,B,C,D
- Create the file:
If the devops.txt
file doesn't exist, you can create it using the touch
command:
- Add content to the file:
You can use a text editor to add the content to the file. For example, using nano
:
Press Ctrl
+ O
to save the changes and Ctrl
+ X
to exit nano
To show only the top three Alphabets from the file.
The head
command is used to display the beginning (or top) lines of a file.
This command will display the first three lines of the devops.txt
file, which correspond to the top three alphabets.
The -n
option specifies the number of lines to display, and 3
is the number of lines you want to show.
This will give you the desired output of showing only the top three Alphabets from the devops.txt
file.
To show only the bottom three alphabets from the file:
To display only the bottom three fruits from the devops.txt
file, you can use the tail
command. The tail
command is used to display the end (or bottom) lines of a file.