The Best Linux Commands Every User Should Know (Clear and Simple Documentation)

The Best Linux Commands Every User Should Know (Clear and Simple Documentation)

Nearly 75% of all mobile phones run Android, based on the Linux kernel, and most servers that host websites and other content run on Linux. There are also several Linux distributions that individuals and organizations use to get their work done every day.

Being new to Linux can be overwhelming since the look and feel of the distributions is quite different from what most Windows and macOS users are accustomed to. Furthermore, the command line interface can make learning to use Linux seem like rocket science. 

In this guide, we walk you through basic file manipulation commands and all the best Linux commands that every user should know. While this guide is based on Ubuntu 18.04, you should be able to use the commands on other distros just as easily.

A Brief Look at the Terminal

A terminal or a shell is a program that facilitates the input of commands to the operating system and the subsequent output of results. Most Linux distributions have a GUI terminal, but when Linux was first created, it only had a Command Line Interface to facilitate operation. 

Nearly every distribution of Linux has a minimal version without a GUI for the tech-savvy power-users that do not need a GUI to rely on for their work.

Nevertheless, if you want to access the Terminal on your Linux OS, you can either look for the Terminal in the menu or hit Ctrl+Alt+T. 

Linux Commands for File Creation

Arguably the best way to learn Linux commands is to run them yourself. Let’s create some files to work with, so your real files remain unaffected. To create a directory away from your machine’s home folder (making it a safer place to experiment), run the following commands:

mkdir /tmp/example
cd /tmp/tutorial

 

The first command, “mkdir,” stands for “make directory.” We use an absolute path to ensure that the example directory is created in /tmp. 

If there were no forward slash before “tmp,” the command would attempt to find a “tmp” directory inside the current directory. It would then attempt to create the “example” directory in that directory. If no “tmp” directory exists there, the command will fail.

The second command, “cd,” stands for “change directory,” and the command is the command-line equivalent of navigating to the “tmp” directory and then to the new “example” directory.

Now that you’re in your test area, you can begin creating subdirectories by running:

mkdir folder1 folder2 folder3

 

This command looks different from the earlier mkdir command since we have added three things after the command. These elements (folder1, folder2, and folder3) are the arguments or parameters for the command.

Different commands accept different arguments and a varying number of them. The mkdir command expects the user to pass at least one parameter but can handle multiple parameters.

On the other hand, the cd command accepts either zero or one argument but cannot work with more.

Now that you have created three subdirectories, you can look at them with the list command “ls.” The command displays all of the files in the current directory. You should see folder1, folder2, and folder3 appear when you run it.

Observe how the mkdir command created the new folders in the same directory (the “example” directory). The command does not put folder3 inside folder2 inside folder1 or make any other kind of nested structure. 

However, you can use the following command if you want to create your subdirectories in that manner:

mkdir -p folder4/folder5/folder6

 

After running the ls command, you will see that only folder4 appears to be added to the list. This is because folder5 is inside it, and folder6 is inside folder5.

You could verify this by changing the directory and running the list command again:

cd folder4
ls
cd folder5
ls

 

The “-p” we used in the mkdir command is referred to as a switch or an option. There are many different switches defined for nearly all commands in Linux. The -p switch means “create the parent directories.” 

Switches change how a command works and allow Linux users to use the same command in different ways. However, it’s important to note that the same switches can mean different things for different commands. 

Most switches have a single character preceded by a hyphen, but they can also be longer words with two hyphens behind them.

Using a single character switch enables you to combine multiple switches and modify how the command works. However, not every command allows this.

Understanding these switches can be confusing since many commands do not identify what options will work with them. In other words, whether something is an option or not is indicated only by order of the arguments. 

The takeaway is that switches can take several forms, and you do not need to know about all of them to use Linux. For instance, the following commands do the same task, but knowing one of them would be sufficient to get the job done:

mkdir –parents –verbose dir4/dir5
mkdir -p –verbose dir4/dir5
mkdir -p -v dir4/dir5
mkdir -pv dir4/dir5

 

Now that you understand how to create multiple directories by passing them as parameters to mkdir, you might wonder, how do you create a directory with a space in the name?

Consider this command:

mkdir another folder

 

You can guess what happens when you run the command. It creates two folders, “another” and “folder.” To create a folder with a space in the name, you will need to “escape” the spaces. 

“Escaping” refers to using special codes to instruct the machine that some characters are different from the normal characters.

Here are a few different ways you can escape the spaces and create folders with a space in them:

mkdir “dir 1”
mkdir ‘dir 2’
mkdir “dir 3” “dir 4”
mkdir dir\ 5
mkdir –p “dir 6”/“dir 7”

 

Working with files and folders with spaces in them becomes taxing for Linux users since the characters need to be escaped often. For this reason, you will notice that Linux power users tend to name their files and folders with letters, numbers, hyphens, and underscores rather than spaces.

File Creation with Redirection

You can capture the output of commands in text files using the Linux terminal. The output can also be manipulated further before storing it in a file. 

Doing this is as simple as putting a “>” at the end of the command and typing in the name of the file to which you want the information written. The following command will put the list of files in the current directory into a text file:

ls > list.txt

 

Running this command does not make anything print on the Terminal since the output has been redirected to the file. Running “ls” again will show you that the list.txt file has appeared in the directory.

To look at the content of the file, you can use the “cat” command:

cat list.txt

 

You will notice that the output is not the same as it would appear when you run “ls.” However, it has all the same data, and since it is in a text file, the data can be processed further as required. 

Let’s take a look at the “echo” command:

echo “This is an example text line”

 

Running the command prints out whatever was in the argument within the quotes to the Terminal. You can use the command along with the redirection to create test files, like so:

echo “This is example line 1” > example_1.txt
echo “This is example line 2” > example_2.txt
echo “This is example line 3” > example_3.txt

 

You can verify that the created files have the text in them using the “cat” command. But the command does a lot more than view files. The “cat” command means “concatenate,” which means to link together.

You can pass several filenames to the command, which will output all of them one after another. But you must remember that the output will appear as a single text block. You could test this by running the following command:

cat example_1.txt example_2.txt example_3.txt

 

Linux terminals offer some useful shortcuts to help prevent needless typing of long commands when the files you work with have similar names. These are called the wildcard characters. 

The question mark indicates “any single character” within a file name. On the other hand, the asterisk character indicates “zero or more characters” in the file name. 

Instead of running the cat command above, you could alternatively run:

cat example_?.txt
cat example_*

 

Linux Commands for Moving and Manipulating Files

You will likely use your Linux machine’s GUI to move, rename, and delete files. However, knowing to do all that using commands can help you when you need to make bulk changes or when the files are spread across several folders. We discuss how to do that in detail in this section.

Let’s begin by moving the example_1.txt file into the folder1 directory. You can do this using the “mv” (move) command:

mv example_1.txt folder1

 

To confirm that the task is complete, use the ls command to check that it’s gone from the working directory. Then use the cd command to navigate to folder1 and run the ls command to check that the file’s there. 

Let’s say that you find out that the example_1.txt file should not be in the folder1 directory. You can move it back to the working directory by navigating to folder1, then running:

mv example_1.txt . .

 

The command instructs the machine to move the file into the parent directory. However, there is an easier way to accomplish the same thing. The two dots in the command (. .) represent the parent directory. In the same way, a single dot represents the current directory.

Since we know that folder1 only has one file, you can use the wildcard character “*” to match any filename in that directory. Therefore, you can use the following command to move the file back into the working directory:

mv folder1/* .  

 

The mv command can also be used to move multiple files simultaneously. If a user passes over two arguments to the command, the last one is considered the destination directory. All the other directories or files are considered the files that need to be moved.

Here’s how you can move all our example_n.txt files and folder3 into folder 2:

mv example_* folder3 folder2

 

Reading one argument in the above command at a time will help you work out what the command is doing.

But what happens if we decide that the example_2.txt doesn’t belong to folder2? Instead of folder2, the file may need to be put inside folder6, which is inside folder5, which is inside folder4. 

Since you know the paths of the directories, writing the following command should be easy:

mv folder2/example_2.txt folder4/folder5/folder6

 

It’s worth noting that the mv command allows us to move files into different directories from any directory. The command line boasts this powerful property – regardless of where you are in the file system, you can work with files and folders in different locations.

Since we are using and moving the example_2.txt file a lot, keeping a copy in the working directory may be the right idea. Just like the mv command moves files, the cp command copies the files:

cp folder4/folder5/folder6/example_2.txt .

 

You can also copy files and give them a new name. For instance, you can make a backup copy of the text file by running this command:

cp example_2.txt backup_example_2.txt

 

While having a backup is always great, the choice of filename could be better. Renaming it so the backup always appears next to the original file in a sorted list is the best way to go.

Traditionally in Unix, the command line handles renaming files as “moving” a file from one name to another. Linux has picked up this aspect of Unix, so you can use the mv command to rename files. 

To use the command for this purpose, you will need to pass two arguments: the file to be renamed and the new name of the file:

mv backup_example_2.txt example_2_backup.txt

 

The renaming function of the mv command is not limited to file names. You can also use it on directories, providing Linux users with a solution to sorting out the difficult names with spaces in them. 

You can put the folder name in quotes when passing it as an argument to the mv command. Let’s change the names of the “dir” folders we made earlier:

mv “dir  1” dir_1
mv “dir  2” dir_2
mv “dir  3” dir_3
mv “dir  4” dir_4
mv “dir  5” dir_5
mv “dir 6” dir_6
mv “dir 7” dir_7

 

Running these commands becomes easier if you use the arrow up button to recall the previously run command. After running the first command and using the arrow up button, you can use the left and right arrows to navigate the cursor and change the directory names before running the command.

Linux Commands for Deleting Files and Folders

Before trying any of the commands in this section, double-check that you are not in the home folder by using the pwd command, which returns the absolute path. The absolute path is the path of a folder starting from the root.

When you’re sure that you’re still in the /tmp/example directory, we can get into learning to use the rm (remove) command. To get rid of the example_2.txt file in folder6, you can run: 

rm folder4/folder5/folder6/example_2.txt

 

If you try to remove the empty “dir” series of folders we renamed in the previous section with rm, it won’t work. Running the following command:

rm dir_*

 

Returns an array of errors stating “cannot remove, it is a directory.” This is because of the built-in failproof of the rm command. While you can use it to delete all the files in a directory in a single command, the command does not let you delete directories.

To delete directories, you will need to use the rmdir (remove directory) command to do the job:

rmdir dir_*

 

If any directories are not empty, the command will throw an error. If you recall, the dir_6 folder has the dir_7 folder inside it. The rmdir command only deletes empty folders, and it is designed this way to ensure that you do not accidentally delete the files you need.

However, you can use switches of rm or rmdir to perform what Linux considers dangerous actions. You can use the -p switch with rmdir to delete the dir_6 folder or any other folder with parent directories. 

It’s important to note that when you run rmdir -p dir_6/dir_7, it will first delete dir_7 and then delete dir_6. That said, rmdir still follows its rules even when the -p switch is used and would not delete any files inside of both dir_6 and dir_7 (if there were any).

If you’re certain that you want to delete an entire directory and everything inside it, you can set rm to work recursively. You can do this using the -r switch, which instructs the machine to remove both files and folders. 

In other words, to remove dir_6, you can run the following command:

rm -r dir_6

 

It is important to remember that deleting the files in a directory, going back to the parent directory with cd . . and then using rmdir to remove the directory is considered best practice since it is safer.

Other Important Linux Commands Users Should Know

Here is a list of other command commands used when operating Linux machines:

touch: It is used to create empty files of various types, including text and zip files. You can create a text file by running “touch plainfile.txt.” However, the touch command cannot create directories. 

man: The man command stands for “manual,” and you can run it to learn more about a command. It displays the manual pages of the command. For instance, running “man cd” will show you all the details and switches of the cd command. You can also use the –help switch instead of man to see the same manual pages.

locate: This command is used to find files in a Linux system. You can think of it as an alternative to the search command in Windows. The locate command is invaluable when you do not know where a file is or do not remember the name of the file. Let’s assume you’re looking for a file with the word “friend” in its name. Running “locate -i friend” will instruct the Linux system to print all the files on the machine with “friend” in them. The -i switch tells the machine to ignore the case of the entered word. If you remember two words of the file’s name, you can run the same command and separate them with an asterisk. If the file has the words “friend” and “program” in it, you can run “locate -i *friend*program.” 

nano, vi, jed: Nano, vi, and jed are the text editors you can use in the Linux command line. Vi and Nano are typically pre-installed on Linux machines. You can use the nano command when programming since it highlights the keywords in different colors and works with most languages. It allows you to create and modify files and offers shortcuts to save your files. But if you’re looking for a text editor for HTML files, nano won’t work well because of the color it adds to the text. Using jed is the right way to go for working with HTML. On the other hand, if you want a simpler text editor, you can use vi. 

sudo: Perhaps the best-known Linux command, sudo means “SuperUser Do.” This is the command to use if you want to run any command with root privileges. For instance, if you need to edit a config file on your machine, you will need root permissions. You can run “sudo nano filename.config” to get the necessary privileges. Alternatively, you can enter the root command line, so all the commands you run have root permissions by running “sudo bash.” You will need to enter the user password to use this command. You can also use “su” instead of “sudo bash,” but you will need to set a root password before using the command. Setting a root password is as simple as running “sudo passwd” and entering the password.

df: The df command displays the available disk space on every partition of your Linux machine. Running the command will allow you to see all the mounted partitions and the used/available space ratio in percentages and KBs. To show the details in megabytes, you can use the switch -m. 

du: You can use the du command to learn the disk usage of a file on the machine. To find the disk usage of a specific file or folder, type the command du followed by the name of the file or folder. For instance, to find the disk space of the Documents folder in Linux, type the command “du Documents.” 

ping: The ping command comes in handy when you want to check your connection to a server. For instance, you can type “ping facebook.com” to check whether your machine is able to connect to the server and elicit a response. The ping command will measure the time taken for the response and provide all the relevant details. Most users use this command to test their internet connection.

tar: The tar command is helpful when working with tarballs (compressed files) in the Linux command line. This command can be used in many different ways. You can use it to compress and decompress a variety of archive files by passing the right parameters. For instance, the “tar -cvf” command will create a tar archive, and the -xvf switches will untar an archive. 

apt-get: The apt command works with packages in the Linux CLI, whereas the apt-get command is used to install packages. Using this command demands root permissions; therefore, you will need to use the sudo command. To install a program, you need to run “sudo apt-get install,” followed by the name of the package you want to install on your machine. It is best practice to update the machine’s repository whenever you want to install a package. This is done by running “sudo apt-get update.” To upgrade your Linux system, you can run “sudo apt-get upgrade.” There are several variations of this command that allow you to accomplish an array of different tasks. 

chmod: The chmod command makes a file executable, but it is also used to change the permissions granted to the executable. Let’s assume you have a Python program named “sorting.py.” You will need to run the command “Python sorting.py” every time you want to run the program. However, you can also make the file executable by running “chmod +x sorting.py.