Linux is the most common and robust free with an open-source operating system used by not only developers but also by system administrators. To excel in the field of information technology and advance your career in IT one must learn Linux since it is an essential skill.
This article lists the top 25 basic commands of Linux along with examples. So folks, let’s just get started with these top 25 commands of Linux.
Top 25 Linux Commands
If you want to learn the basics of Linux and you are a newbie then you must know the following basic commands of Linux. Now we are going to briefly explain the top 25 basic commands of Linux one by one.
ls
This command lists down all the contents of a present directory. You just need to open a terminal by using the shortcut key i.e. CTRL+ALT+T and run the command as shown below:
The following command is used if you want to list the files from a specific folder.
Syntax
Example
The below picture shows the list of files that reside in the Documents folder.
Subsequently, there is another option i.e. “-l”. It will print out all the files along with corresponding file permissions.
Syntax
Example
You can also use several options with this command:
ls –R command is used to list out all the contents of subdirectories.
Example
ls -al command is used to list down detailed information (size, permission, owner) of all the files and directories.
Example
echo
“Echo” is another topmost command of Linux used to display text or string and mostly used in bash or scripts files. It assists to depict environmental variables present in the system. The syntax of the “echo” command is provided below:
Syntax
Example
It is also used to move text/data into a file. Sometimes the user wants to add text within a file then just type the command as shown below. In this example, information.txt is the name of the file in which text i.e. “Hey, Linux Hunters” is added.
Syntax
Example
Output:
touch
The “touch” command is a commonly used command to create files. The syntax of the touch command is shown below:
Syntax
If you want to create multiple files simultaneously then use the following syntax as mentioned below:
Example
In this example, we created two files named employeefile.txt and student file.txt
mkdir
The “mkdir” is an abbreviation of make directories. If you want to create multiple folders simultaneously then you have to use “mkdir” command. We can create one or multiple directories using this command. Below syntax will be used to create a directory.
Example
For this instance, if you want to create a directory named directoryLinux then run the below-mentioned command. Then we use “ls” command to see the directory named directoryLinux.
If you want to create multiple directories simultaneously then use the following syntax as mentioned below:
Example
In the example below, we created multiple directories by using the single “mkdir” command. Then we use the “ls” command to see the directories named dir1, dir2, and root here.
grep
The “Grep” command is used to search specific keywords in a text file. Before using the “grep” command, you must ensure that text files contain some piece of text. The syntax of “grep” command is as follow:
Example
In the example below, we searched the word “Lorem” in the name.txt file then it gives the following lines which contain the “Lorem” word.
man
If you don’t know about the functionality of any command then the “man” command plays an important role and gives you a brief explanation about the functionality of the command. The syntax of “man” command is mentioned below:
Example
If you want to check man for touch command then run the following command in the terminal.
Output:
Pwd
No doubt, the “pwd” Linux command is helpful for daily use. The “PWD” is an abbreviation of the present working directory. If you want to display the present working folder then you have to use the “pwd” command. “Pwd” also provides the name of an absolute path.
Example
cd
The “cd” is an abbreviation of change directory. If you want to switch your current directory into another directory then you have to use this command. If you want to switch your present folder into another folder then use the below-mentioned syntax.
Example
The following command is used to make Documents as the current directory.
Then you can display the path of the current directory by using the “pwd” command as shown below:
Example
The command of moving back to the directory is as follows:
Example
The command of checking the present working directory is as follows:
Example
mv
The ‘mv’ is used to move the directory or file from one place to another and is also used to rename files.
Syntax
Example
In this example, we have a file named employeefile.txt, we will use the ‘mv’ command to rename this file. Subsequently, we used the “ls” command to check the changes.
The following command is used to move file student file.txt from one directory into another directory. Then we used the “cd” command to switch directories. Subsequently, we used the “ls” command in order to check whether the file moved into the directory or not.
Syntax
Example
rmdir
The “rmdir” is an abbreviation for removing directories. If you want to delete multiple directories simultaneously then you can either use the “rmdir” command or “rm” command. This command is helpful for cleaning up and organizing files.
Syntax
Example
In this example, we first used the “ls” command to view files altogether. It contains several directories then we use the “rmdir” command to remove two directories named “linuxdir” and “dir2”. We again used the “ls” command to check whether directories were removed or not.
$ rmdir linuxdir dir2
locate
This command is used to search a file or a directory anywhere in the machine. The syntax of this command is as follows:
In this example, we want to find studentfile.txt then we use the locate command to search this file.
The output of this command is as follows:
less
If you want to view files quickly then you have to use the “less” command. It allows you to read a text file in an existing window, prevents it from opening in the editor’s tool. The syntax of this command is as follows:
The following command is used to view the file named studentfile.txt.
The output of this command is as follows:
compgen
It is one of the handiest Linux commands which helps to display names of all commands as well as functions.
The output of this command is as follows which shows the list of all commands which you can use in the command-line interface.
Sudo command
“Super User Do” is an abbreviation of Sudo. This command is used to do numerous tasks which require privileges of administrative or root. A user password is mandatory whenever you execute “sudo” command in the terminal. This command is not useful for daily purposes as it generates errors easily. You can use the below-mentioned syntax to execute various tasks.
Example
In this example, we want to install mlocate then we use the Sudo command as mentioned below.
The output of this command is as follows:
Cat
The “Cat” command is a commonly used command to create or concatenate files. The “Cat” command is used to show the content of the file.
Syntax
Example
The following command is used to display the content of studentfile.txt:
If you want to input text into a file then just type $ cat > filename. Subsequently, type the text and then press CTRL+D to override the content of the file.
Ping command
“Packet Internet Groper” is an abbreviation of “Ping”. If you want to check the status of connectivity across the server and host then you have to use this “ping” command. This command also supports troubleshooting. The syntax of this command is shown below:
In this example, we used a ping command to check connectivity with google.
The output of this command is as follows:
Head
Head command is most commonly used to read the top 10 lines of the file. It helps you to quickly get the concept of what this file is all about. The syntax of this command is as follows:
The output of this command gives you the first 10 lines of namefile.txt.
Tail
This command is more or less similar to the head command but the “tail” command displays the last 10 lines of a file rather than the first 10 lines of a file.
Syntax
The command of showing the last 10 lines of the file is as follows:
If you want to display more lines of a file then the following command will be used.
Where n represents the number of lines you want to read.
Chmod
If you want to set or edit file or directory permissions then this command is used. The syntax of the “chmod” command is shown below:
This command is used to remove edit permission from the name.txt file.
The output of this command shows that it is a read-only file.
Exit
If you want to exit the terminal itself then the “exit” command is used.
Output
History
This command is used to display all the commands you used for various purposes in the terminal.
The output shows the list of commands which are used recently.
Clear
This is another most widely used Linux command. It is used whenever you want to erase all the data from the terminal.
After you press enter, then you will see a clean terminal as shown below.
Cp
If you want to copy files then this command is used. This command usually takes two parameters i.e. source and destination of the file.
Example
Run the below-mentioned command to copy the file “name.txt” into the dir1. Then we used the “ls” command to view either the file moved in that folder or not.
kill
This command is used to eliminate all running processes in a terminal. You can easily terminate a process by using either process ID (PID) or name. At first, you have to find out all the running processes in a machine. The basic syntax to find out all running processes is shown below.
In this example, we want to terminate PID 1, we will use the following command:
If you want to terminate the process by using its name then use the following command. In this example, we kill the process of the Firefox browser.
sleep
If you want to delay the process for a particular duration of time then the “sleep” command is used. Time can be represented either in days, minutes, or seconds.
In this example, the command will take 6 seconds to execute.
Conclusion
Linux commands assist users in order to execute tasks more efficiently. In this article, we explain the top basic 25 commands for the ease of users. If you are a new user of Linux, then you can master it by learning various Linux commands. Examples are also provided which helps you understand the better usage of each command.