Most Used Linux Commands

Linux has many commands, and luckily you don’t need to know them all. The Linux commands mentioned below will help you navigate Linux with ease.

In case you were wondering –

The list below is technically a mix of both Linux and GNU Core Utilities. Which-is-which is beyond the purpose of this post, but feel free to read more about it here. The GNU Core Utilities is free software that makes it easier to work with Linux. Click here to see the source code behind these utilities. Who knows? Maybe you can contribute code to help the open-source cause.

CD (Change Directory)

The cd Linux command is vital to navigating the Linux platform. It can be used by any user, whether you’re an administrator or a regular user.

cd command:
cd /directory/subdirectory

This will change your current working directory to /directory/subdirectory.

Use the following command to switch back to the parent directory:

cd ..

Now use the following command to go directly to home directory:

cd ~

LS (List)

The ls Linux command is probably the most used command. Chances are you will be using this command first when using Linux for the first time. The ls command is used to list all the directory contents, but that’s not all. You can pair it with other commands to view hidden files, sort files, etc.

ls command:
ls

This command gives you the names of all the files residing in your directory. It does not provide you with metadata about the files, such as type, size, etc.

ls -l command:
localhost:~# ls -l
total 16
-rw-r--r--    1 root     root           114 Jul  6 03:47 bench.py
-rw-r--r--    1 root     root            76 Jul  3 19:15 hello.c

This command gives you extra detail about the directory contents as well. They include the modified date, size, time, owner name, owner permissions, and file name. All this is presented in the form of a list.

ls -a command:
localhost:~$ ls -a
.             ..            .ash_history  .screenrc     .xsession     sample1       sample2

The ls -a command is used to view all the hidden files. It displays a list of all the files, including hidden files. The files starting with a ‘.’ are used to identify hidden files.

ls -lh command:
localhost:~# ls -lh
total 16K
-rw-r--r--    1 root     root         114 Jul  6 03:47 bench.py
-rw-r--r--    1 root     root          76 Jul  3 19:15 hello.c

This command is like ls -l command with a small difference. It outputs all the files and their details in a human-readable format. The visible change is the changed format of file size.

ls -r command:
localhost:~# ls -r
readme.txt  hello.js    hello.c     bench.py

This is a simple command which outputs all the files and directories in reverse order. The reverse order is based alphabetically.

ls -R command:
localhost:/# ls -R
.:
bin    etc    lib    mnt    proc   run    srv    tmp    var
dev    home   media  opt    root   sbin   sys    usr
 
./bin:
arch            df              gzip            mkdir           ps              stty
ash             dmesg           hostname        mknod           pwd             su

ls command alone lists on the names of main directories. It does not display subdirectories. The ls -R command is used to view all subdirectories.

ls -lS command (lowercase l, uppercase S):
localhost:/# ls -lS
total 52
drwxr-xr-x    4 root     root          2240 Aug  8 07:38 dev
drwxr-xr-x    2 root     root          1998 Jul 12 08:34 bin
drwxr-xr-x    2 root     root            37 May 30 00:20 opt
dr-xr-xr-x   12 root     root             0 Aug  8 07:38 sys

In case you wish to find which directory has the largest file size, then this command is useful. It lists out all the directories in the order of largest file size to smallest.

MKDIR (Make Directory)

You may well know about the different directories already present on your Linux. What if you wanted to create new directories? Well, this command is built for that. The mkdir command is used to make single or many directories. You can also set permissions for each directory you create. Remember that not every user has permission to create directories. You must have the right privileges before you create any directory, or you’ll receive a “permission denied” error.

mkdir directory_name command:
mkdir Sample

This is the most straightforward command without any options. Executing this command will create a folder named “Sample” under the directory you’re currently working in. If you’re on the parent directory, it will create the “Sample” folder under the parent directory. Use the ls command to check if the directory has been created or not.

mkdir {dir1,dir2,dir(n)} command:
mkdir {sample1,sample2,sample3,sample4}

To save you some time, you also have the option to create many directories at once. Use the code above to create as many directories as you want at once. After mkdir, enter the names of directories in curly brackets, separated by a comma. Executing the command will create all the directories at once under the current directory you’re in.

RM (Remove)

Deleting files or directories in a graphical interface is easy. When it comes to the command-line interface, you’ll need to know commands if you want to delete a file. In Linux, we use the rm command to delete directories or files.

To put it clearly, the rm command deletes all references of that file from the file system. So, you can’t get back the file once it is deleted. There are ways to recursively delete files and folders, but let us stick to the basics first.

rm file_name command:
rm tax.txt

When we execute the rm command without any options, by default, it doesn’t prompt the user and doesn’t delete directories. You must have permission to the parent directory to execute this command. Here when we execute this command, the file named “tax” is deleted forever. You can use any file name if it exists.

rm file1 file2 file(n) command:
rm tax.txt finance.txt accounting.txt

You can delete many files at once by typing their names after the rm command separated by space. Here we are deleting the files “tax, finance, and accounting” at once. The terminal will delete them forever without prompting.

You can also watch this great video for more tips on deleting files and folders:

PWD (print working directory)

The pwd Linux command prints out the current working directory.

pwd command:
localhost:~$ pwd
/home/anotherUser

This simple command returns the current working directory. For different users, it displays a different directory. It shows the directory at which the user is currently present.

pwd -L command:
localhost:/home$ pwd -L
/home

The pwd -L command prints the value of pwd if it names the current working directory.

pwd -P command:
localhost:/home$ pwd -P
/home

This command is used to print the physical directory without any symbolic links. It avoids all symbolic links.

CAT (Concatenate)

As the name suggests, it is used to concatenate different files. The cat command allows you to create a view contain a file, single file, multiple files, redirect output in files, and concatenate files.

The general syntax for cat is:

cat [option] [file]

First, we’ll be creating files using the cat command.

cat >sample command:

cat >any_file_name

After this terminal will wait for you to enter some data. Type in some text or number and press Ctrl + D together. Your text will be written in your new named file. Do this again with another file name. Suppose you have created two files named sample1 and sample 2.

Now let’s see how we can view the contents of the file.

Cat file_Name command:
localhost:~$ cat sample1
This is some text

This command displays the text or data stored in the file.

Now suppose you wish to view data of multiple files:

localhost:~$ cat sample1 sample2
This is some text
This is some text

Write the names of your file separated by a space. The terminal will output the content of files in the order you called them.

You also have the option to view the contents of a file in a numbered list format. Use the following code, and all the contents returned will be in a numbered sequence:

cat -n file_name

With the cd command, you also have the power to copy contents from one file into another. Keep in mind that copying data from one file into another overrides the data. Create two different files. Now we’re going to show you how to copy data from file1 into file2.

Cat Piping
cat file1 > file2

Replace file1 with the name of the file whose content you want to copy, and replace file 2 with the name of the destination file. The data on file2 will be overwritten by file1.

Now, what if you want to keep the contents of a file and copy data into the same file? The ‘cat file1 > file2’ command does not allow you to append data. Don’t worry. There’s another command which allows you to append data to the destination file. Use the following command:

cat file1 >> file2

All of the data from file1 will be appended into file 2.

CP (Copy):

The cp command is one of the most used commands in Linux. You can use the cp command to copy a file, many files, or even directories. In this post, we’ll teach you how to use the cp command for files and directories.

cp file1 file2 command:
cp source_file destination_file

You must have a source file filled with data from where you wish to copy. A destination file is not necessary. If a destination file exists, the command will override its existing data. If the destination file does not exist, the command will first create the file and then copy the data into it.

cp file1 file2 file(n) directory command:
cp sourcefile1 sourcefile2 sourcefile3 Documents

You can use this command to copy many files into a specific directory. Running this command will copy all three files into directory Documents. To check it, you can open Documents via Files.

cp -R source_dir destination_dir command:
cp -R Documents Test

Sometimes you may need to copy a whole directory into another. This command is used to copy all the contents of a directory to another directory. When you run it, the Documents directory and all its contents will be copied to the Test directory.

MV (Move)

Now it’s time to learn how to move directories and files around. Mv stands for move, and this command moves files and directories from one place to another in Linux. It serves another function. You can also rename a file with mv command. Let’s look into how you can use this command.

mv old_name.txt new_name.txt command:
mv tax.txt record.txt

You can use the mv command in this sequence to rename a file. Here we are renaming the file called “tax” to a new name “record.” This command doesn’t rename a file, but rather it moves the old file and renames it as the new file under the same directory. Now you’ll find “record.txt” under the directory you’re working in.

mv file_name directory_name command:
mv test.txt Desktop

If you wish to move a file to a certain directory, then follow the above command. Use the mv command followed by the file’s name you wish to move and then write the directories name to move the file. Here we are moving the file names “test” to the directory called Desktop.

GREP (Global regular expression Print)

You may have a file with hundreds of entries in it. You may need to search for a specific keyword, sentence, name, number, etc. This is where the grep command comes in. You can use the command to search for a string of characters in any file. It finds the string and outputs all the lines containing that string. The string or term you search for is called regular expressions. Let’s look at how you can use this command.

grep -i “pattern” file_name command:
grep -i "password" bank.txt

Here we are searching the file name bank.txt for a term called “password.” The grep command will print out all the lines that contain the word “password” in them. The -i is used for a case insensitive search.

grep -c “pattern” file_name command:
grep -c "debit" tax.txt

Sometimes you may need to know how many times a word is written in a file. Here we are counting how many times the word “debit” is used in the tax.txt file. The command will only output a digit of how many times “debit” appeared in tax.txt.

grep -l “pattern” file1 file2 file(n) command:
grep -l "How to do taxes" bank.txt tax.txt intro.txt

The -l option is used to display all the files that contain an expression. Here we are searching for a sentence in three different files. The command will output the names of all the files where the sentence is found.

KILL

There may arise a situation where a process becomes stuck. The process won’t be halted, so you may need to force close it. In this case, you can use the kill command. The kill command terminates a process. Let’s look at how to do so.

kill PID (Process ID) command:
kill 1603

You can use this command to stop a process. All you have to do is enter the processes PID. The PID can be found by using the ps command. Here the process with PID 1603 will be terminated.

SU (Substitute User)

Like windows, there can be more than one user using Linux. All of them have different rights to access data. There is a single superuser who can access all the data, and then there are sub-users. At any time, you can switch between users using the su command.

su user_name

Replace user_name with the name of the user who you want to switch to. If the user is associated with a password, you’ll need to enter its respective password to gain access. Using this command has a problem. The user switches with this command but retains the environment from the original login user. Thus, the switched user can’t access files as they belong to the initial user.

To switch user as well as the environment, use any of these commands:

su user_name
su -l user_name
su --login user_name

All of them have the same functionality. Using this command will also ask you for a password related to that user. After you enter the correct password, the user is switched and the files related to them. Now the user will be able to access their files.

sudo (Substitute User, Do)

You know that there are different users with different privileges and access rights. For example, the root user has the highest privileges. And, sometimes you need to execute commands as root or another user. The sudo command allows you to do this.  Sudo stands for “substitute user, do”. In other words, you switch to another user like root and then do the command.

Remember when you run a program as administrator in Windows OS? The sudo command is it’s equal, generally speaking. Any user with fewer rights can execute a command as a root user with the sudo command in a nutshell. Let’s look at some examples.

Note that not all users are allowed to use sudo.  Users need to be added to the sudo group to grant them sudo permissions.

sudo -l command:
root@server:/var/log# sudo -l
Matching Defaults entries for root on merlin:
    env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User root may run the following commands on merlin:
    (ALL : ALL) ALL

The sudo -l command allows you to see what root commands you can execute.

sudo !! command:
sudo !!

Using the sudo !! command allows you to execute your previous command using if you forgot to use sudo before.

sudo -u [user] [command]
sudo -u Anthony [command]

The sudo -u command allow you to run a command as another user, in this case, it is: Anthony. The command option indicates the command you want to run as another user.

sudo su command
sudo su

Use sudo su if you want to switch to the root account.

sudo command
sudo [command]

The sudo command, without -u allows you to run your command as the root user. The command option indicates the command you want to run as another user.

sudo vs. su –

You have an idea of what the Sudo and Su command is, but what is the difference? S

When you execute su, it switches to the root user. You are required to enter the Root user’s password to switch to the root user. One more thing to keep in mind is that the environment remains the same with su command. It doesn’t change to the root’s environment. For example, suppose a user “test” runs the su command. Now “test” will be switched to the root user, but the environment will remain tests.

There’s a variation of the su command that is “su -. “ This command also switches the user to the root user but also creates a new environment. The rest is the same. Using this command is beneficial as you don’t have to log out, and then re-login as the root user.

Some differences:

  1. With the sudo command, you can only run a single command. To run more commands, you have to use the sudo command. The su – command creates a new environment, and you can execute any number of commands you want.
  2. When you use the sudo command, you have to type in the current user’s password. When you use su – command, you have to type in the root user’s password.
  3. You can trace a user when they execute a command using sudo. But, you can’t trace the user when they use su – command.
  4. There is no limit for the user’s when using the su – command. They can run any command they want. With the sudo command, users can be limited to what commands they can run and what they can’t.

History

The bash shell stores a history of executed commands by different users. The history Linux command can be used for audit purposes or to view what commands were executed. The syntax is super simple, just type ‘history’.

history command:
localhost:~$ history
   0 ls
   1 su anotherUser
   2 su -l anotherUser
   3 cat /etc/passwd
   4 su user2

Executing this command will print out a list of all the commands you previously executed in a numbered form.

In some cases, different users execute numerous numbers of commands. It becomes a hassle to search through the whole list. There is a command you can use to view the history of commands, one page at a time. This command allows you the ease to traverse through commands by pressing the space bar to view the next page or using the down arrow key to view one command at a time.

Input the following command:

history | less

Sometimes you don’t want the whole list of commands. You just want the last few executed commands. To view the list of last ten commands, use the following code:

history | tail

Finally, to view the list of last 25 executed commands use the following code:

history 25
How to view the time and date of commands

Sometimes you may need to view the date as well as the time for commands. Using the following command, you can print a list of all the executed commands and their date and time.

export HISTTIMEFORMAT=' %F %T'
How to delete the history of commands

For some reason, you may want to remove a certain command from history, or even delete all of the histories. Once you delete command or all commands from history, you can’t get them back, so keep in mind.

Use the following command to delete a particular command:

history -d <line_number>

Insert the line number where your command resides, which you wish to delete.

Now enter the following Linux command into the terminal to delete the complete history:

history -c

ID (Identity)

There can be different users using the server. The id Linux command can be used to find out any user’s effective user id, group information, and supplemental groups.

Running this command will output effective user id, group information, and supplemental groups about the current user.

localhost:~$ id
uid=1001(anotherUser) gid=1001(anotherUser) groups=1001(anotherUser)

id [AnotherUser]

To view data about another user, use the following command:

localhost:~$ id root
uid=0(root) gid=0(root) groups=0(root),0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),11(floppy),20(
dialout),26(tape),27(video)

CHOWN (Change Owner) & CHMOD (Change Mode)

You may be dealing with files of different users. One user does not have permission to use the files of another user. Only the administrator can access the file of all the users. Ownership of a file belongs to the user who created it. Here, we will teach you how to copy ownership and permissions of one file to another. Before that, let’s discuss some basics. But read our post called: Basics of Using chown and chmod Commands, if you want more detailed information.

Let’s look at how to view ownership and permissions of files.

Say you wish to view permission and ownership of a certain file names sample2. Use the following command to do so:

ls -l sample2

This will display who has permissions and ownership to the particular file.

How to copy ownership:

To copy ownership of a file to another file or multiple files, we use the chown command. In the code, we use –reference switch with the following syntax.

chown --reference=main_file file

Here the –reference acts as a switch. Instead of main_file, type the file name of which you want to copy ownership. Next, instead of a file, replace it with the file name you want to transfer ownership to.

The result will be that owner of main_file will be copied to file.

To copy ownership to multiple files, use this command:

chown --reference=main_file file1 file2 file3
How to copy permissions:

If you wish to copy one file permissions to another, then you have to use the same –reference switch chmod command. One from where you want to copy permission, and one file where you want to copy permissions to. Use the following syntax.

chmod --reference=main_file file

Replace the main_file with the original file from where you want to copy permission. Finally, replace the file with the file name where you want to copy permissions to.

The permissions of main_file will be copied to file.

REBOOT

Reboot command in Linux does what a Restart command does in Windows. It shuts down the system and restarts it. There are many reasons why you would need to use this command. After installing major updates, you need to reboot the system. The system needs rebooting to make the user’s changes effective on a server. Keep in mind that not every user has the ability to reboot the system. You must have root access or enough privileges. With the command-line interface, you can restart your system with a straightforward command. Let’s look at how.

reboot command:
sudo reboot

Before you execute the following command, make sure you’re a root user, or you have the permissions to use the command. Next, type in the command and hit enter. Your system will close, and after a few seconds, it will start again.

If you’re not logged in as a root user, you can use the ‘sudo reboot’ command. This command will execute the reboot command as a root user. The terminal will ask for the root’s password. After entering the password, hit enter to reboot your system.

TAIL

This simple command is used to output the last part of any file. Simply speaking, the tail command prints the last few lines. It can be used by default or paired with an option. When the tail command is used without any options, it outputs the last ten lines of the specified file.

Tail is a very versatile command. You can specify how many last lines you wish to print. The tail command can also print all the lines after a specified line number. You can print the last characters and even print the last parts of many files. Let’s look at examples to understand this command better.

tail file_name command:
tail names.txt

Like we stated before when we use it without options, it returns the last ten lines. Here, the command will do the same by returning the last ten lines of file “names.”

tail -n file_name command:
tail -3 names.txt

What if we want to output three or four or however many lines? In this case, we can use the -n option. Just replace -n with any number such as -3. Upon execution of this command, the terminal will print the last three files from “names.”

tail -f command:
tail -f names.txt

Now let’s look at the ‘follow’ option paired with the Tail command. The Follow option will continuously return the last ten lines of the file and do so when new data that is added to the file. This command runs forever, and you have to use the interrupt key to abort the command. The command will also inform you if the file decreases in size.

SERVICE

Let’s start with the basics. A service is a program that functions in the background. We are interested in using basic commands such as start, stop, and restart for services. Services can also be called daemons. These services or daemons are stored under /etc/init.d.

In a nutshell, the service command is a service manager. In the latest distros, the developers have changed the command to systemctl. Don’t worry; the service commands also work as it is redirected to systemctl command automatically. Let’s look at examples to understand this command better.

service service_name start command:
service myService start

The syntax to start any service is very straightforward. Type in the $service command followed by the name of the service you want to start and the end type “start.” Here we are starting the ‘myService’ service. Executing the command will not display anything but the service ‘myService’ will start in the background.

service status service_name status command:
service myService status

We have learned how to start a service, but how will we know if it has started or not? We can know its status using the status option. Executing the above command will output whether the service is active or inactive.

Final thoughts

It takes time to remember all these Linux commands, but you will survive if you keep to the basics.

Which Linux command should we add next on Anto Online? Let us know in the comments.



About the Authors

Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic!

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee



7 Comments on “Most Used Linux Commands”

Leave a Reply

Your email address will not be published. Required fields are marked *