Basic Linux/Unix Commands with Examples
1.alias
The ‘alias’ is another name for a command. If no argument is given, it shows current aliases. Aliases can be used for short names of commands. For example, you might use the cd command frequently. You can create an alias for it:
$ alias CD=’cd Desktop’
2.cal
Displays the calendar of the current month.
$ cal
November 2020
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
3.cat
The ‘cat’ command is actually a concatenator but can be used to view the contents of a file.
$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
4.cat
Change the current working directory to the directory provided as argument. If no argument is given to ‘cd’, it changes the directory to the user’s home directory. In the given example change the directory to ‘/’.
$ cd /
5.chgrp
The chgrp will change the group of the file.
$ chgrp red file1
6.chown
The chown will change the owner of the file.
$ chown visitor file1
7.Clear
This command clears the screen.
$ clear
8.copy
Copy files and directories. If the source is a file, and the destination (file) name does not exit, then source is copied with new name i.e. with the name provided as the destination.
$ copy source destination
If the destination is a directory, then the file is copied with its original name in that directory.
$ cp backup.txt folder1
$ ls -l folder1
total 0
-rw-r–r–. 1 root root 0 Nov 8 02:28 backup.txt
9.date
Displays current time and date.
$ date
Sun Nov 8 02:33:35 IST 2020
10.df
The df reports file system usage. For example,
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 480828 0 480828 0% /dev
tmpfs 497840 0 497840 0% /dev/shm
tmpfs 497840 8684 489156 2% /run
tmpfs 497840 0 497840 0% /sys/fs/cgroup
/dev/mapper/centos-root 17811456 4615632 13195824 26% /
/dev/sda1 1038336 216240 822096 21% /boot
tmpfs 99572 28 99544 1% /run/user/1000
/dev/sr0 4669162 4669162 0 100% /run/media/pardhan/CentOS 7 x86_64
11.df
The du command determines disk usage of a file. If the argument given to it is a directory, then it will list disk usage of all the files and directories recursively under that directory:
$ du
0 ./.mozilla/extensions
0 ./.mozilla/plugins
0 ./.mozilla
0 ./.cache/gdm
8 ./.cache/imsettings
0 ./.cache/libgweather
0 ./.cache/evolution/addressbook/trash
0 ./.cache/evolution/addressbook
0 ./.cache/evolution/calendar/trash
0 ./.cache/evolution/calendar
0 ./.cache/evolution/mail/trash
$ du /etc/passwd
4 /etc/passwd
12.echo
This command will echo whatever you provide it.
$ echo “hello world”
hello worlds
13.fdisk
The fdisk is a tool for getting partition information, and for adding and removing partitions. The fdisk tool requires super user privileges. To list all the partitions of all the hard drives available:
$ fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0009329d
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
The fdisk is an interactive tool to edit the partition table. It takes a device (hard disk) as an argument, whose partition table needs to be edited.
$ fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
14.grep
The ‘grep’ command searches for a pattern in a file (or standard input). It supports regular expressions. It returns a line if it matches the pattern in that line. So, if we wish to find the lines containing the word ‘nologin’, we use ‘grep’ as follows:
$ grep nologin /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
15. file
The file command determines the file type of a given file. For example:
$ file /etc/passwd
/etc/passwd: ASCII text
16.halt
This command in Linux is used to instruct the hardware to stop all the CPU functions. Basically, it reboots or stops the system.
$ halt
17.head
Displays the first few lihaltnes of a file. By default, the ‘head’ command displays the first 10 lines of a file. But with -n option, the number of lines to be viewed can be specified.
$ head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
18.history
This command shows the commands you have entered on your terminal so far.
$ history
1 SU –
2 su –
3 alias CD=’cd Desktop’
4 clear
5 cal
6 cat /etc/passwd
7 cd
8 cd /
9 clear
10 cd
11 chown visior file
12 useradd visitor
13 ll
14 touch file1
15 useradd visitor
16 chown visitor file1
17 grpadd
18 groupadd red
19 su –
20 date
21 df
22 dn
23 du
24 du /etc/passwd
25 echo “hello world”
26 fdisk -l
27 su –
28 su –
29 head /etc/passwd
30 history
19.help
With almost every command, ‘–help’ option shows usage summary for that command.
$ date –help
Usage: date [OPTION]… [+FORMAT]
or: date [-u|–utc|–universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.
20.id
This command prints user and groups (UID and GID) of the current user.
$ id root
uid=0(root) gid=0(root) groups=0(root)
21.info
Info documents are sometimes more elaborate than the man pages. But for some commands, info pages are just the same as man pages.
$ info date
22.last
Displays information about the users who logged in and out of the system. A similar command is ‘lastb’ that shows the last unsuccessful login attempts. But this command must be run as root otherwise you would get an error saying permission denied.
$ lastb
root pts/0
root pts/0
Sun Nov 8 07:45 – 07:45 (00:00)
Sat Nov 7 11:29 – 11:29 (00:00)
btmp begins Sat Nov 7 11:29:39 2020
23.ln
The ln command is used in linux to create links. Links are a kind of shortcuts to other files. The general form of command is:
$ ln TARGET LINK_NAME
25.ls
List files and/or directories. If no argument is given, the contents of current directory are shown.
$ ls
anaconda-ks.cfg backup.txt folder1 initial-setup-ks.cfg
25.man
To see a command’s manual page, man command is used.
$ man date
26.mkdir
To create a directory, the ‘mkdir’ command is used.
$ mkdir clado
27.move
Move files or directories. The ‘mv’ command works like ‘cp’ command, except that the original file is removed. But, the mv command can be used to rename the files (or directories).
$ mv source destination
$ mv folder1 clado
28.netstat
The ‘netstat’ is a command used to check the network statistics of the system. It will list the current network connections, routing table information, interface statistics, masquerade connections and a lot more information.
$ netstat | head
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ]
unix 3 [ ]
unix 2 [ ]
unix 5 [ ]
unix 24 [ ]
unix 3 [ ]
29.pwd
DGRAM 14215
DGRAM 9194
DGRAM 9196
DGRAM 9207
DGRAM 9209
STREAM CONNECTED
/run/systemd/shutdownd
/run/systemd/notify
/run/systemd/cgroups-agent
/run/systemd/journal/socket
/dev/log
40577
‘pwd’ command prints the absolute path to current working directory.
$ pwd
/home/pardhan
30.reboot
reboot command is used restart or reboot the system.
$ reboot
31.remove
remove command is used to remove a file or directory. A directory must be removed recursively with -r option.
$ rm –r clado
32.stat
To check the status of a file. This provides more detailed information about a file than ‘ls -l’ output.
$ stat file1
File: ‘file1’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 17668947 Links: 1
Access: (0664/-rw-rw-r–) Uid: ( 1000/pardhan) Gid: ( 1000/pardhan)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2020-11-08 01:09:53.005082195 +0530
Modify: 2020-11-08 01:09:53.005082195 +0530
Change: 2020-11-08 01:09:53.005082195 +0530
Birth: –
33.sudo
sudo (Super User DO) command in Linux is generally used as a prefix of some command that only superuser are allowed to run.The -V (version) option causes sudo to print the version number and exit.
$ sudo -V
Sudo version 1.8.23
Sudoers policy plugin version 1.8.23
Sudoers file grammar version 46
Sudoers I/O plugin version 1.8.23
34.tail
The ‘tail’ command shows the last 10 lines by default, and -n option is available as well.
$ tail -n 4 /etc/passwd
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
pardhan:x:1000:1000:Pardhan:/home/pardhan:/bin/bash
[pardhan@MyFirstLinuxVM ~]$
35.touch
For creating an empty file, use the touch command.
$ touch file3
$ ls -l
total 4
drwxrwxr-x. 2 pardhan pardhan 6 Nov 8 09:28 clado
-rw-rw-r–. 1 pardhan pardhan 0 Nov 8 01:09 file1
-rw-rw-r–. 1 pardhan pardhan 0 Nov 8 10:13 file2
-rw-rw-r–. 1 pardhan pardhan 0 Nov 8 10:14 file3
36.tty
Displays current terminal.
$ tty
/dev/pts/
37.which
w command is used to check which users are logged in to the system, and what command they are executing at that particular time.
$ w
10:30:14 up 1:08, 2 users, load average: 0.34, 0.13, 0.09
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
pardhan :0 :0 09:23 ?xdm? 1:57 0.52s /usr/libe
pardhan pts/0 :0 09:27 6.00s 0.73s 0.27s w
38.wc
Word count
This command counts lines, words and letters of the input given to it.
$ wc /etc/passwd
43 88 2275 /etc/passwd
39.whatis
This command gives a one line description about the command. It can be used as a quick reference for any command.
$ whatis cal
cal (1)
cal (1p)
40.whoami
– display a calendar
– print a calendar
This command reveals the user who is currently logged in.
$ whoami
pardhan