0% found this document useful (0 votes)
10 views

1.1 Basic of Linux Commands

The document provides an overview of Linux commands, detailing their usage and syntax for both basic and advanced tasks. It categorizes commands into sections such as Directory, File, User, Filter, and Networking commands, with examples for each. The document serves as a practical guide for beginners and professionals to navigate the Linux command-line interface effectively.

Uploaded by

s3itsol
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

1.1 Basic of Linux Commands

The document provides an overview of Linux commands, detailing their usage and syntax for both basic and advanced tasks. It categorizes commands into sections such as Directory, File, User, Filter, and Networking commands, with examples for each. The document serves as a practical guide for beginners and professionals to navigate the Linux command-line interface effectively.

Uploaded by

s3itsol
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Linux Commands with Examples

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:

Linux Directory Commands


Linux File Commands
Linux File Content Commands
Linux User Commands
Linux Filter Commands
Linux Utility Commands
Linux Networking Command

Linux Directory Commands

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.

Syntax: mkdri directoryname

Output:-
sarath@ubuntu18:~$ mkdir dbasession
sarath@ubuntu18:~$
2. pwd Command

The mkdir command is used to create a new directory under any directory.

Syntax: mkdir directoryname

Output:-
sarath@ubuntu18:~$ mkdir dbasession
sarath@ubuntu18:~$

3. rmdir Command

The rmdir command is used to delete a directory.

Syntax: rmdir directoryname

Output:-

sarath@ubuntu18:~$ rmdir dbasession


sarath@ubuntu18:~$

4. ls Command

The ls command is used to display a list of content of a directory.

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

The cd command is used to change the current directory.

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.

Syntax: touch file1


touch file2 file3 file3
sarath@ubuntu18:~/dba$ touch file1
sarath@ubuntu18:~/dba$ touch file2 file3
sarath@ubuntu18:~/dba$ ls
file1 file2 file3
sarath@ubuntu18:~/dba$

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.

1. cat > file_name


// Enter file content

Press "CTRL+ D" keys to save the file. To display the content of the file, execute it as
follows:

sarath@ubuntu18:~$ cat > demo.txt


i am sarath, my email [email protected]
sarath@ubuntu18:~$ cat demo.txt
i am sarath, my email [email protected]
sarath@ubuntu18:~$

8. rm Command

The rm command is used to remove a file.

Syntax:

rm file_name

sarath@ubuntu18:~$ rm t1
sarath@ubuntu18:~$ rm tq file1
sarath@ubuntu18:~$
9. cp Command

The cp command is used to copy a file or directory.


Syntax:
To copy in the same directory
cp <existing file name> <new file name>
To copy in a different directory:
cp <existing file name> <new Location>

sarath@ubuntu18:~$ cp demo.txt demo_test.txt


sarath@ubuntu18:~$ cp demo.txt /home/sarath/Documents

11. rename Command

The rename command is used to rename files. It is useful for renaming a large group of files.
Syntax:

1. rename 's/old-name/new-name/' files

For example, to convert all the text files into pdf files, execute the below command:

1. rename 's/\.txt$/\.pdf/' *.txt

Linux File Content Commands

12. head 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

sarath@ubuntu18:~$ head demo.txt


1,i am sarath, my email [email protected]
2,Student list
3,mamata mysql
4,kishore linux
5,kiran oldfriend
6,ramya friend
7,The head command is used to display the content of a file.
9,cat command is a multi-purpose
10,the content of one file 9,to another file, and more.
11,file_name

13. tail Command


The tail command is similar to the head command. The difference between both commands is
that it displays the last ten lines of the file content. It is useful for reading the error message.

Syntax: tail file_name

sarath@ubuntu18:~$ tail demo.txt


9,cat command is a multi-purpose
10,the content of one file 9,to another file, and more.
11,file_name
12,Enter file content
13,Press "CTRL+ D" keys to save the file. To display the content of the file, execute it as follows:
14,test the script into sarath@ubuntu18
15,i am sarath, my email [email protected]
16,sarath@ubuntu18:~$ cat demo.txt
17,i am trainer for mysql and linux
18,contact me with [email protected]

14. tac Command

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

sarath@ubuntu18:~$ tac demo.txt

18,contact me with [email protected]


17,i am trainer for mysql and linux
16,sarath@ubuntu18:~$ cat demo.txt
15,i am sarath, my email [email protected]
14,test the script into sarath@ubuntu18
13,Press "CTRL+ D" keys to save the file. To display the content of the file, execute it as follows:
12,Enter file content
11,file_name
10,the content of one file 9,to another file, and more.
9,cat command is a multi-purpose
7,The head command is used to display the content of a file.
6,ramya friend
5,kiran oldfriend
4,kishore linux
3,mamata mysql
2,Student list
1,i am sarath, my email [email protected]

15. more command

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

sarath@ubuntu18:~$ more test

Linux User Commands

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)

18. useradd Command


The useradd command is used to add or remove a user on a Linux server.

Syntax:
useradd username
root@ubuntu18:~# useradd oracle

19. passwd Command

The passwd command is used to create and change the password for a user.

Syntax:

passwd username

root@ubuntu18:~# passwd oracle


Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@ubuntu18:~#

20. groupadd Command

The groupadd command is used to create a user group.

Syntax:
groupadd <group name>
root@ubuntu18:~# groupadd developer
root@ubuntu18:~#

Linux Filter Commands

21. cut Command

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.

Syntax: cut -d(delimiter) -f(columnNumber) file_name

Example 1

sarath@ubuntu18:~$ cat > marks


alex-50
sara-0
kiran-40
ramya-60
sravya-40
devi-30
latha-40
To Save Ctrl+D
siri-70sarath@ubuntu18:~$ cut -d- -f2 marks
50
0
40
60
40
30
40
70
sarath@ubuntu18:~$ cut -d- -f1 marks
alex
sara
kiran
ramya
sravya
devi
latha
siri

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

22. grep Command

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

26. sed command

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

28. uniq Command

The uniq command is used to form a sorted list in which every word will occur only once.

Syntax:

command fileName | uniq

sarath@ubuntu18:~$ sort student | uniq


Sjaanu*97
YVjaanu*99
chandu*99
jaanu*69
kiran*40
radha*40
suma*90
yeshoda*76
sarath@ubuntu18:~$ cat student | uniq
kiran*40
suma*90
radha*40
yeshoda*76
chandu*99
jaanu*69
Sjaanu*97
YVjaanu*99

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:

1. od -b <fileName> // Octal format


2. od -t x1 <fileName> // Hexa decimal format
3. od -c <fileName> // ASCII character format
sarath@ubuntu18:~$ od -b marks
0000000 141 154 145 170 055 065 060 012 163 141 162 141 055 060 012 153
0000020 151 162 141 156 055 064 060 012 162 141 155 171 141 055 066 060
0000040 012 163 162 141 166 171 141 055 064 060 012 144 145 166 151 055
0000060 063 060 012 154 141 164 150 141 055 064 060 012 163 151 162 151
0000100 055 067 060

31. sort Command

The sort command is used to sort files in alphabetical order.

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

32. gzip Command

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

33. gunzip Command

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

Linux Utility Commands

34. find Command

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

35. locate Command

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:~$

36. date Command

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

37. cal Command


The cal command is used to display the current month's calendar with the current date
highlighted.
Syntax:
cal

37. sleep Command

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

38. zcat Command

The zcat command is used to display the compressed files.


Syntax:
zcat file
sarath@ubuntu18:~$ ls
db demo.txt file2 marks.gz mysql.pdf student test1 tools.pdf
dba demo_test.txt linux.pdf moral.pdf oracle.pdf test.gz test2
sarath@ubuntu18:~$ zcat marks.gz
alex-50
sara-0
kiran-40
ramya-60
sravya-40
devi-30
latha-40
siri-70
bhanu
banu
chitti

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

40. exit Command

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.

41. clear Command

Linux clear command is used to clear the terminal screen.


Syntax:
clear
sarath@ubuntu18:~$ clear
After pressing the ENTER key, it will clear the terminal.

42. ip Command

Linux ip command is an updated version of the ipconfig command. It is used to assign an IP


address, initialize an interface, disable an interface.
Syntax:
ip a or ip addr
sarath@ubuntu18:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:22:48:44:c6:d4 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.8/24 brd 10.0.0.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::222:48ff:fe44:c6d4/64 scope link
valid_lft forever preferred_lft forever

43. ssh 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.

44. mail Command

The mail command is used to send emails from the command line.
Syntax:

mail -s "Subject" recipient_address


sarath@ubuntu18:~$ mail -s "Hello Friend" [email protected]

45. ping Command

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

--- google.com ping statistics ---


3 packets transmitted, 3 received, 0% packet loss, time 2007ms
rtt min/avg/max/mdev = 18.076/18.775/19.979/0.869 ms
sarath@ubuntu18:~$

46. host Command

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.

You might also like