All Linux commands are case sensitive. The commands get executed when you press enter key after typing the command. The output of the command will be shown in the terminal itself.
Linux includes a lot of commands. In this Linux commands cheat sheet, I will take you through the most important ones so that you get familiar with them and can easily work on the Linux terminal.
To execute the commands, you will need a computer running Ubuntu or any other Linux distros. In Ubuntu, you can launch the terminal by pressing button Ctrl-Alt-T or by clicking the ‘Activities’ button at the top left of the screen and typing ‘shell’ or ‘terminal’ or other common synonyms.
If you don’t have Linux OS, but you want to practice Linux commands, you can use any online Linux terminal. Many Linux terminals are available online free of cost to practice Linux commands.
Complete List Of Linux Commands
Basic Linux Commands
#1) pwd: pwd command prints the current working directory. It will give you the absolute path i.e. from the root till the current directory in which you are in. This command helps you avoid getting lost in the terminal window. You will know in which directory you are.
By default, the present working directory is the home directory when you have opened the terminal for the first time.
#2) whoami: This command will display the username for the user who is currently logged in to the Linux terminal.
#3) clear: When the terminal gets filled up with lots of commands and their output, you can use clear command to clear up your terminal.
#4) exit: exit command will simply close off the terminal session.
Linux Commands To Display Date, Time, Calendar
#1) date: This command displays the current time and date.
#2) cal: cal stands for the calendar. By default, the cal command will display the calendar of the current month. At the time of writing this article, the current month was March 2020 and hence in the output, you can see that it has shown March 2020 calendar.
- If you want to display the calendar of the whole current year, you can give command ‘cal -y’.
- You can also specify the year. For Example, if you want to see the calendar of 2019, you can give command ‘cal 2019’ – This will display the calendar of the year 2019.
- Also, if you want to see the calendar of a specific month of a specific year, say January 2019, you can execute command ‘cal 1 2019’ – this will show January 2019 calendar. Here, ‘1’ denotes the first month of the year i.e. January.
- You can also see the calendar of three months together –the previous month, current month, and next month. The command for this operation is ‘cal -3’
Commands To Navigate The Linux File System
The Linux file system has a tree-like structure which is also called a directory tree. It is used to organize information.
The directory tree is very similar to a family tree and so it's a directory or a file having the parent. However, each directory or a file has exactly one parent.
Now, we will take a look at how the Linux file system is structured. First, we have the root directory which is the first or the top-most directory in our tree. It is represented by a forward slash (/). Under the root, you will find many subdirectories.
Let us look at some important subdirectories:
- Bin: This directory contains executable programs and commands that can be used by all the users on the system.
- Opt: This stands for optional. This directory contains commercial software products that are not installed by default on your system. For Example, if you are using ubuntu, you must have noticed that Google Chrome is not installed by default on your system and if you want to install Google Chrome, you will find it located underneath the Opt directory.
- Temp: temp stands for temporary. In this directory, you will find temporary files.
- Var: It contains variable data that frequently changes over time. These include log files, email pools, and user databases.
- Home: Linux is a multi-user environment which means that many users can access a system simultaneously. Each user is allocated a dietary inside the home directory.
Now, let us learn commands that will help us to navigate the directory tree.
(i) pwd: We have already discussed this command above in detail.
(ii) ls: ls stands for list.
The syntax for ls command is:
ls [OPTIONS] [FILES]
This command lists the directory content. If you simply execute ls command without giving any options and files, it will list in alphabetical order all the directories and files that are underneath your current working directory.
You can use the ls command with various options and arguments:
a) ls -l: This shows the content with a detailed listing. -l stands for the long option.
b) ls -lh: It is used to display the human-friendly file sizes. -h stands for humans.
c) Ls -lha: This command is used to display all hidden files. -a includes hidden files.
Example for these commands:
d) ls /: As / represents a root directory, this command will show all the files and folders present inside the root directory.
e) ls ~: As ~ represents the home directory, so this command will display the contents of your home directory.
Command: ls ~
f) ls ..: This command will display the contents of the parent directory because .. represents the parent directory. Suppose, right now I am in the test directory inside my home directory. If I execute command ls .. here then it will display the contents of the parent directory of test which is my home directory.
Commands executed in below example:
g) You can also list the contents of a file by giving its absolute pathname.
Command: ls /home/cpf657/kaushapx/test/parent
Similarly, you can also use a relative pathname.
(iii) cd: Another useful command in navigating through the filesystem is cd. cd stands for change directory.
Suppose I am in my home directory right now and want to navigate to a directory named ‘test’ which is present inside my home directory, so I will use cd test command:
To go to a level up, you need to execute cd .. command:
You can also navigate to any directory using its absolute pathname. For Example, suppose that right now I am in the test directory under my home. Now, I want to switch to another directory called test1 which is present inside my home directory. So, I will use the cd command along with the absolute path of the test1 directory.
There are many options that we can use with the cd command.
cd /: The first option that is very useful is space forward slash. So, as you know that Slash represents the root directory, cd / will take you directly to the root directory.
cd ~ : The second option that you can use is cd Tilda i.e. cd ~. This will take you to the home directory as you can see below screenshot.
cd ..: Another option that you can use with cd command is cd two dots i.e. cd ..
The two dots represent the parent directory, so you will go one step backward in the directory tree by using cd .. command:
If you type cd two dots again and again and again, you will eventually go to your root directory.
If you want to go to some other directory inside the parent directory, then also you can use this command like below:
cd –: the cd space dash command takes you to the previous working directory.
Other Useful Linux Commands
(i) w: This command will display the information on currently logged in users.
(ii) uname: This command displays the information on the Linux computer on which you are working. There are different options with which you can use this command.
- uname -a: To display all the information about the Linux computer.
Command: uname -a
- uname -s: To display the type of kernel.
Command: uname -s
- uname -r: To display the kernel release information.
Command: uname -r
- uname -v : To display the kernel version.
Command: uname -v
(iii) top: This command will display the real-time data about your Linux machine. It shows the status summary, for how long the computer is running, average load, number of tasks running, CPU information, memory information, the status of processes, etc. You need to press Q to exit from top command.
(iv) tar: tar stands for tape archive. This command is used for creating an archive file comprised of many other files, or it can also be used for extracting files from an archive file.
First, let us see how to create an archived file. Suppose, I am currently at a directory called ‘parent’. Inside this parent directory, I have another directory called ‘directory1’ which contains two text files as shown in the below screenshot.
Now, I will use the tar command to archive all the files in directory1 using the below command:
Command: tar -cvf testarchive.tar directory1/
As you can see in the above screenshot that I have used three options: -c -v and -f.
- -c is the create option which is used to create the archive file.
- -v is the verbose option which will list the files in the terminal window that have been added to the archive window, as you can see in the above screenshot.
- -f option is used to give the desired name to the archive file. The filename should follow immediately after -f option.
In the above example, the archive file is created, but it's not compressed.
There are two options to compress the archive file. These are:
-z: This is the gzip option. It instructs the tar to use gzip utility to compress the archive file. It offers decent compression and reasonable speed. It’s better to add the suffix ‘.gz’ after the filename so that when someone tries to extract files from the archive, they can easily get to know the kind of compress.
Command: tar -cvzf testarchive.tar.gz directory1/
-j: This is the bzip2 option. It uses a superior compression algorithm for compression. It offers better compression and slower speed. It’s better to add ‘.bz2’ the suffix after the filename so that when someone tries to extract files from the archive, they can easily get to know the kind of compress.
Command: tar -cvjf testarchive.tar.bz2 directory1/
Now, let’s discuss the command for extracting files from an archive file. We need to use -x i.e. extract option for this purpose. The -v and -f options behave in the same way in this case as they did for the archive.
Command: tar -xvf testarchive.tar
For extracting the files from a .tar.gz archive, you need to use -z option and for extracting the files from. tar.bz2 archive, you need to use -j option.
tar -xvzf testarchive.tar.gz
tar -xvjf testarchive.tar.bz2
(v) tail: This command is used to enlist the last 10 lines of a file. By default, it shows the last 10 lines, but if you want to specify the number of lines then you can use -n option.
The syntax is tail <filename> , or tail -n <number of lines> <filename>
Command: tail DemoFile.txt
Command: tail -n 5 DemoFile.txt
(vi) sudo: sudo stands for the superuser do. This command is used to perform the actions that need root or superuser permissions, like changing the password for another user, etc.
(vii) ps: This command enlists the processes that are running in the current shell.
If you want to see the processes running for a particular user, you need to use -u (user) option in the command.
Command: ps -u kaushapx
To display every process that is running, you need to use -e (every) option i.e. ps -e in the command.
(viii) ping: This is a highly useful command for troubleshooting network issues. It allows you to check whether you have network connectivity with another network machine.
The syntax for this command is ping <ip_address_of_machine or device_name>
You need to press Ctrl+C to stop the output of the ping command.
For Example, let us try to ping google's website to check whether its reachable or not.
Command: ping www.google.com
You can see in the above example that the Google IP address 184.108.40.206 has responded to the ping request and started sending packets of 64 bytes.
The sequence numbers through ICMP protocol (seq=1, seq=2, and so on) tells you that there are no missed responses or packet loss. The ttl is the time to live for a packet and this is mainly for avoiding any network flooding issues.
Time is the duration it takes for the request to reach from your computer to google and then back to your computer. The shorter it is, the faster it is. In the end, the highlighted part is the summary which shows the ping statistics that can be used to monitor any network connectivity issues.
If you want to ping for a specific number of times only, you can use -c (count) option. And, If you want to hear a ping, you can use -a (audible) option. For Example, ping -ac 6 www.google.com
(ix) passwd: This command allows you to change the password. If you are changing it for yourself you just need to type passwd command alone. But, if you are changing it for someone else, you need to use this command along with sudo command.
For changing your own password: passwd
For changing another user password: sudo passwd <username>
Command: sudo passwd mary
(x) mv: This is a very important command. mv stands for move. This command lets you move files and folders from one directory to another. You can also rename the files with the help of this command.
For moving a file to a new location in your filesystem,
Syntax: mv [filename] [destination-directory]
Command: mv DemoFile.txt /home/cpf657/kaushapx/test/parent/directory2
If you just want to rename a file, then you can use the following syntax.
Syntax: mv [filename] [new_filename]
Command: mv DemoFile.txt Hello.txt
You can also achieve file move and rename in one go as can be seen in the below example:
Command: mv Hello.txt /home/cpf657/kaushapx/test/parent/directory1/DemoFile.txt
(xi) mkdir: mkdir stands for make directory. This command is used for creating new directories in the filesystem.
Syntax: mkdir [options…] [directories …]
If you want to create a new directory inside the current directory, you can give only the directory name in the command. Otherwise, you need to give the path of the new directory.
Some options that can be used with mkdir command are as follows:
- –version: It displays the version and license information.
Command: mkdir –version
- –help: This shows all the information regarding mkdir command.
Command: mkdir –help
- –v: This is the verbose option. It will display a message for the created directory.
Command: mkdir -v test3Dir
- -p: This option is used to create parent directories in the case when it does not exist for the newly-created directory.
Command: mkdir -vp parent2/child2
- -m: This option is used to set up permissions for the newly-created directory.
Syntax: mkdir -m a=rwx [directories]
Suppose you only want to give read permission to a directory, then you can use the following command.
Command: mkdir -vm a=r readOnlyDir
You can see in the above screenshot that the readOnlyDir is the directory with read-only permissions (dr–r–r–)
(xii) alias: This command is used to give your name to a command or a combination of commands.
Syntax: alias [-p] [name[=value] … ]
For Example, if you want to rename mv command to move, you can do it as follows.
Command: alias mv=move
(xiii) cat: cat stands for concatenate. This command reads data from a file and displays its content as the output on the terminal window.
Let us see some variations of this command:
- cat filename: This command will show the contents of a given file as output in the terminal window.
Command: cat LearnLinuxCommands.txt
- cat file1 file2: This command will concatenate the contents of two files and will show in the output.
Command: cat LearnLinuxCommands.txt DemoFile.txt
- cat -n filename: To display the contents of a file along with its line numbers.
Command: cat -n Demofile.txt
- cat >newFileName: This command is used to create a new file.
- cat [source_filename] > [destination_filename]: This command is used to copy the contents of one file to another. Note that it will replace the contents of the destination file with the contents of the source file.
- cat [file1] >> [file2]: This command will append the contents of file1 to the end of contents of file2.
- tac filename: This command will show the contents of a file in reverse order.
- cat -E filename: This command is used to highlight the end of the line.
(xiv) chmod: This command is used to set permissions on a file or directory. If you will perform ls -l on any file or directory, you will see initial characters on the output like -rwxrwxrwx. The first character ‘–‘ tells here that it’s a file if it display ‘d’ then it means that it’s a directory.
The next three characters represent the permissions for the user, the next represents permissions of the group and the last three represent permissions for others. r means to read, w means write and x means execute. If in place of rwx, you anywhere see -, then it means that file permission is not granted.
To set permission, you need to use the chmod command.
There are a total of eight digits that represent different levels of permission.
0: No permission
3: write and execute
5: read and execute
6: read and write
7: read, write and execute
Suppose I want to use read, write and execute permission for myself (7), read-only permission for the group (4) and read & execute permission for others(5), I will use 745.
ls -l DemoFile.txt
chmod -R 745 DemoFile.txt
ls -l DemoFile.txt
(xv) chown: chown stands for change owner. With this command, you can change the owner and a group owner for a file.
Syntax: chown [OPTION]… [OWNER][:[GROUP]] FILE…
If you want to change the owner of the file, you can use this command as:
chown [owner_name] [file_name]
If you want to change group owner, then you will need to use this command with sudo command as:
sudo chown [name_of_owner]:[name_of_group] [filename]
(xvi) df: This command shows the size, used space, available space, use percentage, and mounted on details for the filesystems of your computer.
-h and -x are human-readable and exclude options respectively. -h shows the size in a human-readable format (Gb and Mb). -x option is used to exclude any filesystem that you are not interested in.
In this example below, I have excluded squashfs.
Command: df -h -x squashes
(xvii) diff: diff stands for the difference. This command is used to compare two text files and show differences between them.
Syntax: diff [option] filename1 filename2
Command: diff file1 file2
You can use certain options with this command, like -y for side by side comparison, -W for specifying line width to avoid any wraparounds, –suppress-common-lines for suppressing common lines between two files in the diff output.
diff -y -W 70 file1 file2
diff -y -W 70 file1 file2 –suppress-common-lines
(xviii) echo: echo command will print the text on the terminal window. You can also pass environment variables in echo command, as seen below.
echo Hello World
(xix) find: This command is used to search for files and directories in a filesystem. You can find by providing file name, directory name, create date, modified date, file owner, file permissions, etc. You can also use wildcards in the search expression.
Syntax: find [start search from this point] [search expression] [-options] [what to find]
find . -name “*file*”
In the above example, this command has searched for all the files in the current directory (.), which contains the string ‘file’(*file*) in their file names (-name option).
Try to use find –help to explore the various options that this command provides.
(xx) Kill: This is a built-in command in Linux that is used to terminate a process manually. You need to supply the process id (PID) to the kill command to terminate a process.
Syntax: kill PID
(xxi) history: This command will show the commands that you have previously executed on the command line. To view the history of all the commands, you need to simply type history.
For Example, If I want to see the last 10 commands I have executed, I will use below command as
On the extreme left, you can see the numbers like 1033, 1034,.. These are the event numbers. This can be different for different computers. You can use these event numbers to repeat a specific command.
If you want to repeat last executed command, simply type !!
(xxii) head: The head command shows the top 10 lines of a file. To view the specific number of top lines, use -n option.
Command: head -n 3 file2
(xxiii) gzip: This command is used to compress a file.
Syntax: gzip [-options] filename
gzip -v file1
Use gzip –help to explore various options for gzip command.
(xxiv) Groups: This command displays the group names in which the user is a member.
Syntax: groups username
Command: groups kaushapx
(xxv) free: This command tells us about the RAM and swap memory usage (used, free, shared, buffers, cached) within your computer.
Command: free -h
(xxvi) less: This is a very useful command which you can use to view the contents of a file on the Linux terminal itself without opening the file in the editor.
Syntax: less file_name
You need to press :q to come out from the output of less command.