Linux Commands
Linux Commands
Nishant Soni
Contents
1 Linux Terminal Commands 3
1.1 Say hello to the Linux terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.1 GNOME Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 date command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 cal command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 whoami command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5 id command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.6 pwd command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.7 cd command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.8 . directory and .. directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.9 ls command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.10 mkdir command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.11 rm command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.12 cp command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.13 Renaming or moving a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.14 tree command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.15 cat command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.16 wc command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.17 echo command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.18 Redirecting the command output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.19 Using > to redirect output to a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.20 man command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.21 Counting files in a directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.22 Moving around in the command line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1 Linux Terminal Commands
In Linux the terminal is the lifeline for the software developers, programmers and for any aspiring Linux user.
Anything which can be done on GUI (by clicking on different widgets, icons and buttons) can also be done and
infact much more efficiently on the terminal by using commands. The documentation Linux OS (commands
and usage) can be quite overwhelming for new users and it might not be possible even for the proficient devel-
opers to remember all the commands but with regular usage one can easily master few of the most commonly
used commands.
This document is aimed at introducing the reader to minimal set of basic commands which are required to
Linux operating systems efficiently. Having been said that please keep in mind that it’s just the tip of the
iceberg.
The Linux command line is often referred to as the shell, terminal, console, prompt or various other names,
To follow along this tutorial you will need a computer with Ubuntu or some other version of Linux. Every
Linux system includes a command line of one sort or another. This tutorial includes some specfic steps for
Ubuntu 20.04 but most of the content should work regardless of your Linux distribution.
When you launch the command prompt, a window will pop up which contains the following information,
[username@hostname directoryname]
A screenshot of the Gnome terminal application is shown in Figure 1. In this case, the username is nishant,
hostname is Julia, and directoryname mentioned is ∼. The ∼ is a special character. It means the home
directory of the user. In this case, the home directory path is /home/nishant.
Figure 1
The Gnome terminal is one of many implementations of terminal emulators. Different Linux environments
may come pre-installed with different terminals. Please refer to the Wikipedia articles to learn about computer
terminals, terminal emulators, and shell.
The date command can also save you time if you want to see yesterday’s date, or 21 days ago, or 2 years 2
months 3 days ago etc., you can even do that as well.
If you want to see the calendar of a particular month and year, you can even do that using the following
command,
cal month year
1.4 whoami command
whoami command tells the user which user account they are using on the system.
1.5 id command
id outputs real user id and various other details related to the account.
1.7 cd command
cd command is the short name for change directory. This command helps the user to change his/her current
directory. For example, if you would want to move to the /tmp directory it can be done with cd /tmp.
In the above example, first we moved to /tmp directory, and then we moved back to the home directory by
using ∼ character. If we just write cd without any directory name, it will take us to the home directory, ∼,
by default.
1.8 . directory and .. directory
. and .. has special meaning in the Linux.
1.9 ls command
We use ls command to list the files and directories inside any given directory. If you use ls command without
any argument, then it will work on the current directory by default. Few examples on the usage of ls command
is shown below.
In the last three commands we provided a path as the argument to the ls command. / is a special directory,
which represents root directory in Linux filesystem.
We can also create nested directories in a single command using the -p option.
1.11 rm command
rm command is used to remove a file, or directory. The −r option is being used to remove a folder with
its contents in a recursive way. With −f you force the removal, ignoring errors and never prompt. You can
chain the flags, so instead of rm −r −f you can as well type rm −rf. But, always double check before you
use rm −rf command, if you by mistake give this command in your home directory, or any other important
directory, it will not ask to confirm, but it will delete everything there. So, please be careful and read twice
before pressing enter key.
1.12 cp command
We can use the cp command to copy a file in the Linux shell. To copy a folder with its contents recursively
we use the cp command with the -r flag. To copy an existing file we use the following format :
In the following example, we are copying hello.txt to hello copy.txt in the same directory.
In the following example, I am copying the file FlowThermoLab.jpg from the Pictures directory in my home
directory to the current directory, ∼/C++/Slides/Images. Recall that the current directory absolute path can
be directly specified with a single . and we need not provide the entire path.
In another example, I will be copying the images directory (and everything inside it) from the Downloads
directory under home to the /tmp/ directory.
• Cat command can append the contents of one file to the end of another file.
Command : cat file1 >> file2 Output : Will append the contents of one file to the end of another file
• Cat command can display content in reverse order using tac command.
Command : tac filename
Output : Will display content in reverse order.
1.16 wc command
wc command is the short for word count, is an useful command which can help us to count newlines, words
and bytes of a file.
The | is known as pipe. Please watch this video to know more about pipe operator.
The general syntax is man section command. For example, the command man 1 ls will produce the
output shown below. You will need to press to press q to quit the program.
You can learn about different sections from the man Wikipedia page. A screenshot for the manual sections
is provided below.
1.21 Counting files in a directory
Normally ls commands shows all the files and directories in multiple column. But if you pipe the output to
any another command, then it prints one name in a line. We can combine that with wc -l to count the number
of files in a directory.