1.1 Basic of Linux Commands
1.1 Basic of Linux Commands
The Linux command is a utility of the Linux operating system. All basic and advanced tasks can be
done by executing commands. The commands are executed on the . The terminal is a command-line
interface to interact with the system, which is similar to the command prompt in the Windows OS.
Commands in Linux are .
Linux provides a powerful command-line interface compared to other operating systems
such as Windows and MacOS. We can do basic work and advanced work through its terminal. We
can do some basic tasks such as creating a file, deleting a file, moving a file, and more. In addition,
we can also perform advanced tasks such as administrative tasks (including package installation, user
management), networking tasks (ssh connection), security tasks, and many more.
Linux terminal is a user-friendly terminal as it provides various support options. To open the Linux
terminal, press " " keys together, and execute a command by pressing the ' ' key.
In this topic, we will discuss Linux commands with their examples. These commands are very useful
for a beginner and professional both. We have divided these commands into following sections so
that you can easily identify their usage:
1. pwd Command
The pwd command is used to display the location of the current working directory.
Syntax: pwd
Output:-
sarath@ubuntu18:~$ pwd
/home/sarath
sarath@ubuntu18:~$
2. pwd Command
The mkdir command is used to create a new directory under any directory.
Output:-
sarath@ubuntu18:~$ mkdir dbasession
sarath@ubuntu18:~$
2. pwd Command
The mkdir command is used to create a new directory under any directory.
Output:-
sarath@ubuntu18:~$ mkdir dbasession
sarath@ubuntu18:~$
3. rmdir Command
Output:-
4. ls Command
Syntax:ls
sarath@ubuntu18:~$ ls
libmysqlclient-dev_5.7.32-1ubuntu18.04_amd64.deb mysql-common_5.7.32-
1ubuntu18.04_amd64.deb mysql-community-test_5.7.32-1ubuntu18.04_amd64.deb
libmysqlclient20_5.7.32-1ubuntu18.04_amd64.deb mysql-community-client_5.7.32-
-source_5.7.32-1ubuntu18.04_amd64.deb
sarath@ubuntu18:~$
5. cd Command
Syntax: cd directoryname
sarath@ubuntu18:~$ cd dba
sarath@ubuntu18:~/dba$
Linux File commands
6. touch Command
The touch command is used to create empty files. We can create multiple empty files by
executing it once.
7. cat Command
The cat command is a multi-purpose utility in the Linux system. It can be used to create a
file, display content of the file, copy the content of one file to another file, and more.
Press "CTRL+ D" keys to save the file. To display the content of the file, execute it as
follows:
8. rm Command
Syntax:
rm file_name
sarath@ubuntu18:~$ rm t1
sarath@ubuntu18:~$ rm tq file1
sarath@ubuntu18:~$
9. cp Command
The rename command is used to rename files. It is useful for renaming a large group of files.
Syntax:
For example, to convert all the text files into pdf files, execute the below command:
The head command is used to display the content of a file. It displays the first 10 lines of a
file.
Syntax:
head file_name
The tac command is the reverse of cat command, as its name specified. It displays the file
content in reverse order (from the last line).
Syntax:
tac file_name
The more command is quite similar to the cat command, as it is used to display the file
content in the same way that the cat command does. The only difference between both
commands is that, in case of larger files, the more command displays screenful output at a
time.
In more command, the following keys are used to scroll the page:
ENTER key: To scroll down page by line.
Space bar: To move to the next page.
b key: To move to the previous page.
/ key: To search the string.
Syntax: more file_name
16. su Command
The su command provides administrative access to another user. In other words, it allows
access of the Linux shell to another user.
Syntax:
su user_name
sarath@ubuntu18:~$ su rama
No passwd entry for user 'rama'
17. id Command
The id command is used to display the user ID (UID) and group ID (GID).
Syntax:
Id
sarath@ubuntu18:~$ id
uid=1000(sarath) gid=1000(sarath)
groups=1000(sarath),4(adm),20(dialout),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44
(video),46(plugdev)
Syntax:
useradd username
root@ubuntu18:~# useradd oracle
The passwd command is used to create and change the password for a user.
Syntax:
passwd username
Syntax:
groupadd <group name>
root@ubuntu18:~# groupadd developer
root@ubuntu18:~#
The cut command is used to select a specific column of a file. The '-d' option is used as a
delimiter, and it can be a space (' '), a slash (/), a hyphen (-), or anything else. And, the '-f'
option is used to specify a column number.
Example 1
Example 2
sarath@ubuntu18:~$ cat > student
kiran*40
suma*90
radha*40
yeshoda*76
chandu*99
jaanu*69
sarath@ubuntu18:~$ cut -d* -f1 student
kiran
suma
radha
yeshoda
chandu
jaanu
sriram
sarath@ubuntu18:~$ cut -d* -f2 student
40
90
40
76
99
69
26
The grep is the most powerful and used filter in a Linux system. The 'grep' stands for "global
regular expression print." It is useful for searching the content from a file. Generally, it is
used with the pipe.
Syntax:
command | grep <searchWord>
sarath@ubuntu18:~$ cat student | grep 9
suma*90
chandu*99
jaanu*69
sarath@ubuntu18:~$ cat student | grep jaanu
jaanu*69
Sjaanu*97
YVjaanu*99
23. comm Command
The 'comm' command is used to compare two files or streams. By default, it displays three
columns, first displays non-matching items of the first file, second indicates the non-matching
item of the second file, and the third column displays the matching items of both files.
Syntax:
comm <file1> <file2>
sarath@ubuntu18:~$ comm student marks
alex-50
kiran*40
sara-0
comm: file 2 is not in sorted order
kiran-40
ramya-60
sravya-40
devi-30
latha-40
siri-70
suma*90
comm: file 1 is not in sorted order
radha*40
yeshoda*76
chandu*99
jaanu*69
Sjaanu*97
YVjaanu*99
The sed command is also known as stream editor. It is used to edit files using a regular
expression. It does not permanently edit files; instead, the edited content remains only on
display. It does not affect the actual file.
Syntax:
command | sed 's/<oldWord>/<newWord>/'
sarath@ubuntu18:~$ echo class | sed 's/class/java/'
java
sarath@ubuntu18:~$ echo class | sed 's/7/10/'
class
27. tr Command
The tr command is used to translate the file content like from lower case to upper case.
Syntax:
command | tr 'old' 'new'
sarath@ubuntu18:~$ cat student | tr 'cjya' 'CJYA'
kirAn*40
sumA*90
rAdhA*40
YeshodA*76
ChAndu*99
JAAnu*69
SJAAnu*97
The uniq command is used to form a sorted list in which every word will occur only once.
Syntax:
29. wc Command
The wc command is used to count the lines, words, and characters in a file.
Syntax:
wc file_name
sarath@ubuntu18:~$ wc student
7 8 76 student
30. od Command
The od command is used to display the content of a file in different s, such as hexadecimal,
octal, and ASCII characters.
Syntax:
Syntax:
sort student
sarath@ubuntu18:~$ sort marks
alex-50
banu
bhanu
chitti
devi-30
kiran-40
latha-40
ramya-60
sara-0
siri-70
sravya-40
The gzip command is used to truncate the file size. It is a compressing tool. It replaces the
original file by the compressed file having '.gz' extension.
Syntax:
gzip filename
gzip file1 file2 file3 ..
sarath@ubuntu18:~$ gzip test
sarath@ubuntu18:~$ ls
db dba demo.txt demo_test.txt file2 marks student test.gz test1 test2
sarath@ubuntu18:~$ gzip test1 test2
sarath@ubuntu18:~$ ls
db dba demo.txt demo_test.txt file2 marks student test.gz test1.gz test2.gz
The gunzip command is used to decompress a file. It is a reverse operation of gzip command.
Syntax:
gunzip file1
gunzip file1 file2 file3 ..
sarath@ubuntu18:~$ ls
db dba demo.txt demo_test.txt file2 marks student test.gz test1.gz test2.gz
sarath@ubuntu18:~$ gunzip test.gz
sarath@ubuntu18:~$ gunzip test1.gz test2.gz
sarath@ubuntu18:~$ ls
db dba demo.txt demo_test.txt file2 marks student test test1 test2
The find command is used to find a particular file within a directory. It also supports various
options to find a file such as byname, by type, by date, and more.
The following symbols are used after the find command:
(.) : For current directory name
(/) : For root
Syntax:
find . -name "*.pdf"
sarath@ubuntu18:~$ find . -name "*.pdf"
./moral.pdf
./oracle.pdf
./linux.pdf
./mysql.pdf
./tools.pdf
sarath@ubuntu18:~$ find . -name "*.txt"
./demo_test.txt
./demo.txt
The locate command is used to search a file by file name. It is quite similar to find command;
the difference is that it is a background process. It searches the file in the database, whereas
the find command searches in the file system. It is faster than the find command. To find the
file with the locates command, keep your database updated.
Syntax:
locate filename
sarath@ubuntu18:~$ locate mysqld.cnf
/etc/mysql/mysql.conf.d/mysqld.cnf
/usr/lib/mysql-test/include/default_mysqld.cnf
/usr/lib/mysql-test/suite/ndb/include/ndb_add_node_mysqld.cnf
sarath@ubuntu18:~$
The date command is used to display date, time, time zone, and more.
Syntax:
date
sarath@ubuntu18:~$ date
Sun Mar 28 07:50:56 UTC 2021
The sleep command is used to hold the terminal by the specified amount of time. By default,
it takes time in seconds.
Syntax:
sleep time
sarath@ubuntu18:~$ sleep 5
39. df Command
The df command is used to display the disk space used in the file system. It displays the
output as in the number of used blocks, available blocks, and the mounted directory.
Syntax:
df
df -k ( display in kb’s)
df -h ( display in gb’s)
sarath@ubuntu18:~$ df
Filesystem 1K-blocks Used Available Use% Mounted on
udev 966324 0 966324 0% /dev
tmpfs 196976 680 196296 1% /run
/dev/sda1 30309264 2787720 27505160 10% /
tmpfs 984876 656 984220 1% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
tmpfs 984876 0 984876 0% /sys/fs/cgroup
/dev/sda15 106858 6170 100688 6% /boot/efi
/dev/sdb1 4060864 16380 3818488 1% /mnt
tmpfs 196972 0 196972 0% /run/user/1000
tmpfs 196972 0 196972 0% /run/user/1001
sarath@ubuntu18:~$ df -k
Filesystem 1K-blocks Used Available Use% Mounted on
udev 966324 0 966324 0% /dev
tmpfs 196976 680 196296 1% /run
/dev/sda1 30309264 2787720 27505160 10% /
tmpfs 984876 656 984220 1% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
tmpfs 984876 0 984876 0% /sys/fs/cgroup
/dev/sda15 106858 6170 100688 6% /boot/efi
/dev/sdb1 4060864 16380 3818488 1% /mnt
tmpfs 196972 0 196972 0% /run/user/1000
tmpfs 196972 0 196972 0% /run/user/1001
sarath@ubuntu18:~$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 944M 0 944M 0% /dev
tmpfs 193M 680K 192M 1% /run
/dev/sda1 29G 2.7G 27G 10% /
tmpfs 962M 656K 962M 1% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 962M 0 962M 0% /sys/fs/cgroup
/dev/sda15 105M 6.1M 99M 6% /boot/efi
/dev/sdb1 3.9G 16M 3.7G 1% /mnt
tmpfs 193M 0 193M 0% /run/user/1000
tmpfs 193M 0 193M 0% /run/user/1001
Linux exit command is used to exit from the current shell. It takes a parameter as a number
and exits the shell with a return of status number.
Syntax:
exit
sarath@ubuntu18:~$ exit
After pressing the ENTER key, it will exit the terminal.
42. ip Command
Linux ssh command is used to create a remote connection through the ssh protocol.
Syntax:
ssh user_name@host(IP/Domain_name)</p>
ssh [email protected]
ssh sarath@mysqlserver
After pressing the ENTER key, it will as for password and enter into the remote server.
The mail command is used to send emails from the command line.
Syntax:
The ping command is used to check the connectivity between two nodes, that is whether the
server is connected. It is a short form of "Packet Internet Groper."
Syntax:
ping destination
ping google.com
ping -c 3 google.com [ 3 times it will ping and will display ]
sarath@ubuntu18:~$ ping google.com
PING google.com (172.217.6.110) 56(84) bytes of data.
64 bytes from ord37s03-in-f14.1e100.net (172.217.6.110): icmp_seq=1 ttl=112 time=19.1 ms
64 bytes from ord37s03-in-f14.1e100.net (172.217.6.110): icmp_seq=2 ttl=112 time=18.9 ms
64 bytes from ord37s03-in-f14.1e100.net (172.217.6.110): icmp_seq=3 ttl=112 time=19.5 ms
64 bytes from ord37s03-in-f14.1e100.net (172.217.6.110): icmp_seq=4 ttl=112 time=18.1 ms
64 bytes from ord37s03-in-f14.1e100.net (172.217.6.110): icmp_seq=5 ttl=112 time=19.9 ms
64 bytes from ord37s03-in-f14.1e100.net (172.217.6.110): icmp_seq=6 ttl=112 time=17.3 ms
64 bytes from ord37s03-in-f14.1e100.net (172.217.6.110): icmp_seq=10 ttl=112 time=20.0 ms
64 bytes from ord37s03-in-f14.1e100.net (172.217.6.110): icmp_seq=11 ttl=112 time=15.7 ms
……
sarath@ubuntu18:~$ ping -c 3 google.com
PING google.com (172.217.6.110) 56(84) bytes of data.
64 bytes from ord37s03-in-f14.1e100.net (172.217.6.110): icmp_seq=1 ttl=112 time=18.2 ms
64 bytes from ord37s03-in-f14.1e100.net (172.217.6.110): icmp_seq=2 ttl=112 time=19.9 ms
64 bytes from ord37s03-in-f14.1e100.net (172.217.6.110): icmp_seq=3 ttl=112 time=18.0 ms
The host command is used to display the IP address for a given domain name and vice versa.
It performs the DNS lookups for the DNS Query.
Syntax:
host <domain name> or <ip address>
sarath@ubuntu18:~$ host google.com
google.com has address 172.217.4.110
google.com has IPv6 address 2607:f8b0:4009:805::200e
google.com mail is handled by 40 alt3.aspmx.l.google.com.
google.com mail is handled by 30 alt2.aspmx.l.google.com.
google.com mail is handled by 10 aspmx.l.google.com.
google.com mail is handled by 50 alt4.aspmx.l.google.com.
google.com mail is handled by 20 alt1.aspmx.l.google.com.
sarath@ubuntu18:~$ host dbacentre.com
dbacentre.com has address 162.241.218.43
dbacentre.com mail is handled by 0 mail.dbacentre.com.
sarath@ubuntu18:~$ host redbus.com
redbus.com has address 54.255.171.192
redbus.com has address 52.74.237.246
redbus.com mail is handled by 1 aspmx.l.google.com.
redbus.com mail is handled by 10 alt3.aspmx.l.google.com.
redbus.com mail is handled by 10 alt4.aspmx.l.google.com.
redbus.com mail is handled by 5 alt1.aspmx.l.google.com.
redbus.com mail is handled by 5 alt2.aspmx.l.google.com.