Linux Commands

Top 25 Linux Commands With Examples

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.

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:

$ ls

The following command is used if you want to list the files from a specific folder.

Syntax

$ ls [directory path]

Example

The below picture shows the list of files that reside in the Documents folder.

$ ls ./Documents

Subsequently, there is another option i.e. “-l”. It will print out all the files along with corresponding file permissions.

Syntax

$ ls [directory path] -l

Example

$ ls ./Documents -l

You can also use several options with this command:

ls –R command is used to list out all the contents of subdirectories.

$ ls -R

Example

ls -al  command is used to list down detailed information (size, permission, owner) of all the files and directories.

$ ls -al

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

$ echo "Text"

Example

$ echo "Testing Echo Command"

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

$ echo "text" >> [filename]

Example

$ echo "Hey, Linux Hunters" >> information.txt

Output:

touch

The “touch” command is a commonly used command to create files. The syntax of the touch command is shown below:

Syntax

$ touch [filename]

If you want to create multiple files simultaneously then use the following syntax as mentioned below:

$ touch [filename1] [filename2]

Example

In this example, we created two files named employeefile.txt and student file.txt

$ touch employeefile.txt studentfile.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.

$ mkdir [directory name]

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.

$ mkdir directoryLinux

If you want to create multiple directories simultaneously then use the following syntax as mentioned below:

$ mkdir directoryname1 directoryname2 directoryname3

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.

$ mkdir dir1 dir2 root

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:

$ grep wordtosearch [filename]

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.

$ grep Lorem name.txt

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:

$ man [commandname]

Example

If you want to check man for touch command then run the following command in the terminal.

$ man touch

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.

$ pwd

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.

$ cd [./directory name]

Example

The following command is used to make Documents as the current directory.

$ cd ./Documents

Then you can display the path of the current directory by using the “pwd” command as shown below:

$ pwd

Example

The command of moving back to the directory is as follows:

$ cd ~

Example

The command of checking the present working directory is as follows:

$ pwd

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

$ mv [original file name] [changed filename]

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.

$ mv employeefile.txt employeesdata.txt

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

$ mv [filename] [directoryname/]

Example

$ mv studentfile.txt dir1/

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

$ rmdir [directory name]

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.

$ ls

$ 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:

$ locate filename

In this example, we want to find studentfile.txt then we use the locate command to search this file.

$ locate studentfile.txt

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:

$ less filename

The following command is used to view the file named studentfile.txt.

$ less 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.

$ compgen -c

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.

$ sudo [command]

Example

In this example, we want to install mlocate then we use the Sudo command as mentioned below.

$ sudo apt install mlocate

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

$ cat filename

Example

The following command is used to display the content of studentfile.txt:

$ cat 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.

$ cat studentfile.txt

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:

$ ping [website-url] or [ip address] or [hostname]

In this example, we used a ping command to check connectivity with google.

$ ping gmail.com

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:

$ head filename

The output of this command gives you the first 10 lines of namefile.txt.

$ head name.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.

$ tail filename

Syntax

The command of showing the last 10 lines of the file is as follows:

$ tail name.txt

If you want to display more lines of a file then the following command will be used.

$ tail -n filename

Where n represents the number of lines you want to read.

$ tail -12 name.txt

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:

$ chmod -w filename

This command is used to remove edit permission from the name.txt file.

$ chmod -w name.txt

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.

$ exit

Output

History

This command is used to display all the commands you used for various purposes in the terminal.

$ history

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.

$ clear

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.

$ cp name.txt dir1

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.

$ ps -ef

In this example, we want to terminate PID 1, we will use the following command:

$ sudo kill 1

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.

$ killall firefox

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.

$ sleep

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.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.