Last updated on:

Basic Shell Commands and Text Manipulation Tools

[stextbox id='black' image='null'] To do - You are currently the root user in your terminal. Before proceeding further, type exit and hit the ↵ Enter key. [/stextbox]

Use of Basic Shell Commands

The stty Command

Using this command with the -a switch allows you to identify which combination of keys should be used to control a foreground process:

trainee@ubuntu1604:~$ stty -a
speed 38400 baud; rows 26; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke -flusho -extproc

[stextbox id='black' image='null'] The two most important combinations are intr = ^C and susp = ^Z. The former kills the process whilst the latter suspends its execution. [/stextbox]

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the stty command to view the command line switches. [/stextbox]

The date command

This command's output gives the current system date and time. The command can also be used to set the system date:

trainee@ubuntu1604:~$ date
Fri 30 Sep 15:19:21 CEST 2016

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the date command to view the command line switches. [/stextbox]

The who Command

This command's output shows who is currently connected to the system:

trainee@ubuntu1604:~$ who
trainee pts/0 2016-09-29 20:36 (10.0.2.2)

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the who command to view the command line switches. [/stextbox]

The df Command

This command's output shows the free space on each mounted block device:

trainee@ubuntu1604:~$ df
Filesystem 1K-blocks Used Available Use% Mounted on
udev 230832 0 230832 0% /dev
tmpfs 50028 2864 47164 6% /run
/dev/sda1 9480420 4269160 4706636 48% /
tmpfs 250124 0 250124 0% /dev/shm
tmpfs 5120 4 5116 1% /run/lock
tmpfs 250124 0 250124 0% /sys/fs/cgroup
tmpfs 50028 0 50028 0% /run/user/1000

The units are shown as blocks. In order to humanize the output, it is possible to use the -h switch (also known as a parameter, option or flag):

trainee@ubuntu1604:~$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 226M 0 226M 0% /dev
tmpfs 49M 2.8M 47M 6% /run
/dev/sda1 9.1G 4.1G 4.5G 48% /
tmpfs 245M 0 245M 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 245M 0 245M 0% /sys/fs/cgroup
tmpfs 49M 0 49M 0% /run/user/1000

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the df command to view the command line switches. [/stextbox]

The free Command

This command's output shows the memory usage:

trainee@ubuntu1604:~$ free
total used free shared buff/cache available
Mem: 500252 88116 8820 1460 403316 387464
Swap: 1997820 10604 1987216

The units are shown as blocks. In order to humanize the output, it is possible to use the -h switch:

trainee@ubuntu1604:~$ free -h
total used free shared buff/cache available
Mem: 488M 86M 8.5M 1.4M 393M 378M
Swap: 1.9G 10M 1.9G

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the free command to view the command line switches. [/stextbox]

The whoami Command

This command's output indicates the user name associated with the current effective user ID:

trainee@ubuntu1604:~$ whoami
trainee

Now become the system administrator root:

trainee@ubuntu1604:~$ sudo su -
[sudo] password for trainee: trainee
root@ubuntu1604:~#

[stextbox id='black' image='null'] Important : Note that the password will not be visible. [/stextbox]

Now use the whoami command again:

root@ubuntu1604:~# whoami
root
root@ubuntu1604:~#

[stextbox id='black' image='null'] Important : Note the current effective user ID is root. [/stextbox]

Finally execute the exit command to return as trainee:

root@ubuntu1604:~# exit
logout
trainee@ubuntu1604:~$

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the whoami command to view the command line switches. [/stextbox]

The pwd Command

This command's output shows the current working directory:

trainee@ubuntu1604:~$ pwd
/home/trainee

Command Line Switches

[stextbox id='black' image='null'] To do : Use the help command with pwd option to view the command line switches. [/stextbox]

The cd Command

This command's output changes the current working directory to that specified by the argument:

trainee@ubuntu1604:~$ cd /tmp
trainee@ubuntu1604:/tmp$ pwd
/tmp
trainee@ubuntu1604:/tmp$

Command Line Switches

[stextbox id='black' image='null'] To do : Use the help command with cd option to view the command line switches. [/stextbox]

The ls Command

This commands output lists information about the files in directory specified as an argument. If no argument is specified, the output lists the files in the current working directory:

trainee@ubuntu1604:/tmp$ ls
hsperfdata_root
inode
systemd-private-cd33d40e1d3a4e08a9cde3de3603311e-systemd-timesyncd.service-bxC1MJ

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the ls command to view the command line switches. [/stextbox]

The lsof Command

This command's output shows information about open files:

trainee@ubuntu1604:/tmp$ sudo su -
[sudo] password for trainee: trainee
root@ubuntu1604:~# lsof | more
COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root cwd DIR 8,1 4096 2 /
systemd 1 root rtd DIR 8,1 4096 2 /
systemd 1 root txt REG 8,1 1577232 325621 /lib/systemd/systemd
systemd 1 root mem REG 8,1 18976 266610 /lib/x86_64-linux-gnu/libuuid.so.1.3.0
systemd 1 root mem REG 8,1 262408 266418 /lib/x86_64-linux-gnu/libblkid.so.1.1.0
systemd 1 root mem REG 8,1 14608 266450 /lib/x86_64-linux-gnu/libdl-2.23.so
systemd 1 root mem REG 8,1 456632 266555 /lib/x86_64-linux-gnu/libpcre.so.3.13.2
systemd 1 root mem REG 8,1 1864888 266426 /lib/x86_64-linux-gnu/libc-2.23.so
systemd 1 root mem REG 8,1 138744 266572 /lib/x86_64-linux-gnu/libpthread-2.23.so
systemd 1 root mem REG 8,1 286824 266502 /lib/x86_64-linux-gnu/libmount.so.1.1.0
systemd 1 root mem REG 8,1 64144 266408 /lib/x86_64-linux-gnu/libapparmor.so.1.4.0
systemd 1 root mem REG 8,1 92864 266489 /lib/x86_64-linux-gnu/libkmod.so.2.3.0
systemd 1 root mem REG 8,1 117288 266416 /lib/x86_64-linux-gnu/libaudit.so.1.0.0
systemd 1 root mem REG 8,1 55904 266542 /lib/x86_64-linux-gnu/libpam.so.0.83.1
systemd 1 root mem REG 8,1 252152 266583 /lib/x86_64-linux-gnu/libseccomp.so.2.2.3
systemd 1 root mem REG 8,1 31712 266580 /lib/x86_64-linux-gnu/librt-2.23.so
systemd 1 root mem REG 8,1 23128 266429 /lib/x86_64-linux-gnu/libcap.so.2.24
systemd 1 root mem REG 8,1 130224 266584 /lib/x86_64-linux-gnu/libselinux.so.1
systemd 1 root mem REG 8,1 162632 266398 /lib/x86_64-linux-gnu/ld-2.23.so
systemd 1 root 0u CHR 1,3 0t0 6 /dev/null
systemd 1 root 1u CHR 1,3 0t0 6 /dev/null
systemd 1 root 2u CHR 1,3 0t0 6 /dev/null
--More--

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the lsof command to view the command line switches. [/stextbox]

The touch Command

This command updates the access and modification times of one or several file(s) to the current time. If the file does not exist, the system creates an empty file:

root@ubuntu1604:~# exit
logout
trainee@ubuntu1604:/tmp$ touch test
trainee@ubuntu1604:/tmp$ ls
hsperfdata_root
inode
systemd-private-cd33d40e1d3a4e08a9cde3de3603311e-systemd-timesyncd.service-bxC1MJ
test

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the touch command to view the command line switches. [/stextbox]

The echo Command

This command writes the arguments to the standard output (i.e. the screen):

trainee@ubuntu1604:/tmp$ echo fenestros
fenestros

Command Line Switches

[stextbox id='black' image='null'] To do : Use the help command with echo option to view the command line switches. [/stextbox]

The cp Command

This command is used to copy a source to a destination or multiple sources to a directory:

trainee@ubuntu1604:/tmp$ cp test ~
trainee@ubuntu1604:/tmp$ ls -l ~
total 48
drwxr-xr-x 2 trainee trainee 4096 mai 3 08:03 Desktop
drwxr-xr-x 2 trainee trainee 4096 mai 3 08:03 Documents
drwxr-xr-x 2 trainee trainee 4096 mai 3 08:03 Downloads
-rw-r--r-- 1 trainee trainee 8980 mai 3 07:27 examples.desktop
drwxr-xr-x 2 trainee trainee 4096 mai 3 08:03 Music
drwxr-xr-x 2 trainee trainee 4096 mai 3 08:03 Pictures
drwxr-xr-x 2 trainee trainee 4096 mai 3 08:03 Public
drwxr-xr-x 2 trainee trainee 4096 mai 3 08:03 Templates
-rw-rw-r-- 1 trainee trainee 0 oct. 4 13:31 test
drwxr-xr-x 2 trainee trainee 4096 mai 3 08:03 Videos
-rw-rw-r-- 1 trainee trainee 442 sept. 30 11:35 vitext

[stextbox id='black' image='null'] Note the use of the ~ (tilde) character which is a shortcut to the current user's home directory. In the case of this example : /home/trainee. [/stextbox]

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the cp command to view the command line switches. [/stextbox]

The file Command

This command determines a file type:

trainee@ubuntu1604:/tmp$ file ~/test
/home/trainee/test: empty

[stextbox id='black' image='null'] Important - Note that in the case of the first line of the above output, tha command file is incapable of informing you of the type of file since test is empty. [/stextbox]

Using the > key, redirect the output of echo fenestros into the /home/trainee/test file as follows:

trainee@ubuntu1604:/tmp$ echo "fenestros" > ~/test

Now use the file command once again to determine the file type:

trainee@ubuntu1604:/tmp$ file ~/test
/home/trainee/test: ASCII text

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the file command to view the command line switches. [/stextbox]

The cat Command

This commands concatenate files, or standard input, to standard output. In the case of only one file as an argument, the effective result is to print the file contents to the screen:

trainee@ubuntu1604:/tmp$ cat ~/test
fenestros

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the cat command to view the command line switches. [/stextbox]

The mv Command

This command renames a source to a destination or moves sources to a directory:

trainee@ubuntu1604:/tmp$ mv ~/test .
trainee@ubuntu1604:/tmp$ ls -l ~
total 48
drwxr-xr-x 2 trainee trainee 4096 mai 3 08:03 Desktop
drwxr-xr-x 2 trainee trainee 4096 mai 3 08:03 Documents
drwxr-xr-x 2 trainee trainee 4096 mai 3 08:03 Downloads
-rw-r--r-- 1 trainee trainee 8980 mai 3 07:27 examples.desktop
drwxr-xr-x 2 trainee trainee 4096 mai 3 08:03 Music
drwxr-xr-x 2 trainee trainee 4096 mai 3 08:03 Pictures
drwxr-xr-x 2 trainee trainee 4096 mai 3 08:03 Public
drwxr-xr-x 2 trainee trainee 4096 mai 3 08:03 Templates
drwxr-xr-x 2 trainee trainee 4096 mai 3 08:03 Videos
-rw-rw-r-- 1 trainee trainee 442 sept. 30 11:35 vitext
trainee@ubuntu1604:/tmp$ mv test TeSt
trainee@ubuntu1604:/tmp$ ls -l
total 16
drwxr-xr-x 2 root root 4096 sept. 28 10:34 hsperfdata_root
drwxr-xr-x 2 root root 4096 sept. 29 10:32 inode
drwx------ 3 root root 4096 sept. 29 10:19 systemd-private-cd33d40e1d3a4e08a9cde3de3603311e-systemd-timesyncd.service-bxC1MJ
-rw-rw-r-- 1 trainee trainee 10 oct. 4 13:36 TeSt

[stextbox id='black' image='null'] Note the use of the shortcut . which indicates the current working directory. [/stextbox]

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the mv command to view the command line switches. [/stextbox]

The mkdir Command

This command creates the directory(ies) if it (they) does (do) not exist:

trainee@ubuntu1604:/tmp$ cd ~
trainee@ubuntu1604:~$ mkdir testdir
trainee@ubuntu1604:~$ ls
Desktop Downloads Music Public testdir vitext
Documents examples.desktop Pictures Templates Videos

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the mkdir command to view the command line switches. [/stextbox]

The rmdir Command

This command removes the directory(ies) if it (they) is (are) empty:

trainee@ubuntu1604:~$ rmdir testdir
trainee@ubuntu1604:~$ ls
Desktop Downloads Music Public Videos
Documents examples.desktop Pictures Templates vitext

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the rmdir command to view the command line switches. [/stextbox]

The rm Command

This command removes a directory, empty of not, as well as files:

trainee@ubuntu1604:~$ mkdir testdir1
trainee@ubuntu1604:~$ cd /tmp
trainee@ubuntu1604:/tmp$ echo "fenestros" > TeSt
trainee@ubuntu1604:/tmp$ cd ~
trainee@ubuntu1604:~$ mv /tmp/TeSt ~/testdir1
trainee@ubuntu1604:~$ ls -lR testdir1/
testdir1/:
total 4
-rw-rw-r-- 1 trainee trainee 10 oct. 4 14:17 TeSt
trainee@ubuntu1604:~$ rmdir testdir1/
rmdir: failed to remove 'testdir1/': Directory not empty
trainee@ubuntu1604:~$ rm -rf testdir1/
trainee@ubuntu1604:~$ ls
Desktop Downloads Music Public Videos
Documents examples.desktop Pictures Templates vitext

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the rm command to view the command line switches. [/stextbox]

The sort Command

This command writes a sorted concatenation of all files to standard output:

trainee@ubuntu1604:~$ touch aac abc bca xyz
trainee@ubuntu1604:~$ ls
aac bca Documents examples.desktop Pictures Templates vitext
abc Desktop Downloads Music Public Videos xyz
trainee@ubuntu1604:~$ ls | sort
aac
abc
bca
Desktop
Documents
Downloads
examples.desktop
Music
Pictures
Public
Templates
Videos
vitext
xyz
trainee@ubuntu1604:~$ ls | sort -r
xyz
vitext
Videos
Templates
Public
Pictures
Music
examples.desktop
Downloads
Documents
Desktop
bca
abc
aac

[stextbox id='black' image='null'] Important - Note the use of the | character, called a pipe. A pipe is used to channel the standard output of the command that precedes it into the standard input of the command that follows it. [/stextbox]

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the sort command to view the command line switches. [/stextbox]

The more Command

This command is used to display a long file page by page:

trainee@ubuntu1604:~$ more /etc/services
# Network services, Internet style
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, officially ports have two entries
# even if the protocol doesn't support UDP operations.
#
# Updated from http://www.iana.org/assignments/port-numbers and other
# sources like http://www.freebsd.org/cgi/cvsweb.cgi/src/etc/services .
# New ports will be added on request if they have been officially assigned
# by IANA and used in the real-world or are needed by a debian package.
# If you need a huge list of used numbers please install the nmap package.
tcpmux	1/tcp	# TCP port service multiplexer
echo	7/tcp
echo	7/udp
discard	9/tcp	sink null
discard	9/udp	sink null
systat	11/tcp	users
daytime	13/tcp
daytime	13/udp
netstat	15/tcp
qotd	17/tcp	quote
msp	18/tcp	# message send protocol
--More--(4%)

[stextbox id='black' image='null'] Important - The ↵ Enter key scrolls down line by line, the Space Bar key scrolls down page by page and the Q key comes back to the command line prompt. [/stextbox]

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the more command to view the command line switches. [/stextbox]

The less Command

The less command produces a similar result to that of the more command. Practice using the less command and refer to the help using less –help. Which command seems the most powerful and versatile ?

trainee@ubuntu1604:~$ less /etc/services
# Network services, Internet style
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, officially ports have two entries
# even if the protocol doesn't support UDP operations.
#
# Updated from http://www.iana.org/assignments/port-numbers and other
# sources like http://www.freebsd.org/cgi/cvsweb.cgi/src/etc/services .
# New ports will be added on request if they have been officially assigned
# by IANA and used in the real-world or are needed by a debian package.
# If you need a huge list of used numbers please install the nmap package.
tcpmux 1/tcp # TCP port service multiplexer
echo 7/tcp
echo 7/udp
discard 9/tcp sink null
discard 9/udp sink null
systat 11/tcp users
daytime 13/tcp
daytime 13/udp
netstat 15/tcp
qotd 17/tcp quote
msp 18/tcp # message send protocol
/etc/services

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the less command to view the command line switches. [/stextbox]

The find Command

This command is used to search for a specific file or directory. The default path is the current directory and default expression is -print:

trainee@ubuntu1604:~$ find acc
find: ‘acc’: No such file or directory
trainee@ubuntu1604:~$ find aac
aac

[stextbox id='black' image='null'] Important : Note that when the file cannot be found, the find command informs you clearly. However when the file is found, the find command just prints the name of the file to standard output. [/stextbox]

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the find command to view the command line switches. [/stextbox]

The su Command

This command is used to change the effective user id and group id to that of the user passed as an argument. When executed with no argument, the system assumes the destination user is root:

trainee@ubuntu1604:~$ sudo su -
[sudo] password for trainee: trainee

[stextbox id='black' image='null'] Important : Note that the password will not be visible. [/stextbox]

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the su command to view the command line switches. [/stextbox]

The updatedb and locate Commands

The locate command is used to list files in databases that match a pattern supplied as an argument to the command. The locate command uses a database. This database needs to be created using the updatedb command before using the locate command.

The default database is /var/lib/mlocate/mlocate.db:

root@ubuntu1604:~# ls -l /var/lib/mlocate/mlocate.db
-rw-r----- 1 root mlocate 5293946 oct. 2 07:35 /var/lib/mlocate/mlocate.db

[stextbox id='black' image='null'] Important : For information concerning the database format, please see man 5 locatedb. [/stextbox]

The updatedb command is configured by editing the /etc/updatedb.conf file:

root@ubuntu1604:~# cat /etc/updatedb.conf
PRUNE_BIND_MOUNTS="yes"
# PRUNENAMES=".git .bzr .hg .svn"
PRUNEPATHS="/tmp /var/spool /media /home/.ecryptfs /var/lib/schroot"
PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs shfs sysfs cifs lustre tmpfs usbfs udf fuse.glusterfs fuse.sshfs curlftpfs ecryptfs fusesmb devtmpfs"

Use of these two commands is very simple:

root@ubuntu1604:~# updatedb
root@ubuntu1604:~# locate aac
/home/trainee/aac
/lib/modules/4.4.0-21-generic/kernel/drivers/scsi/aacraid
/lib/modules/4.4.0-21-generic/kernel/drivers/scsi/aacraid/aacraid.ko
/lib/modules/4.4.0-38-generic/kernel/drivers/scsi/aacraid
/lib/modules/4.4.0-38-generic/kernel/drivers/scsi/aacraid/aacraid.ko
/usr/share/app-install/desktop/krita-data:kde4__calligraactive.desktop
/usr/share/mime/audio/aac.xml
/usr/src/linux-headers-4.4.0-21/drivers/scsi/aacraid
/usr/src/linux-headers-4.4.0-21/drivers/scsi/aacraid/Makefile
/usr/src/linux-headers-4.4.0-21-generic/include/config/scsi/aacraid.h
/usr/src/linux-headers-4.4.0-38/drivers/scsi/aacraid
/usr/src/linux-headers-4.4.0-38/drivers/scsi/aacraid/Makefile
/usr/src/linux-headers-4.4.0-38-generic/include/config/scsi/aacraid.h

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the updatedb and locate commands to view their command line switches. [/stextbox]

The whereis Command

This command is used to show the full paths of the executable, the configuration files and the manuals associated with the argument:

root@ubuntu1604:~# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man5/passwd.5.gz /usr/share/man/man1/passwd.1.gz /usr/share/man/man1/passwd.1ssl.gz

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the whereis command to view the command line switches. [/stextbox]

The which Command

This command searches the PATH variable and returns to standard output the first full path associated with the argument:

root@ubuntu1604:~# which passwd
/usr/bin/passwd

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the which command to view the command line switches. [/stextbox]

The uptime Command

This command prints to standard output the current time, the length of time the system has been up, the number of users on the system and the average number of jobs in the run queue over the last 1, 5 and 15 minutes:

root@ubuntu1604:~# uptime
15:18:02 up 1 day, 13:10, 1 user, load average: 0,00, 0,03, 0,0

Command Line Switches

The switches associated with this command are:

[stextbox id='black' image='null'] To do : Use the –help option of the uptime command to view the command line switches. [/stextbox]

The w Command

This command outputs the same data as the uptime command on the first line and then complements this information with the details of each user connected to the system, including what each user is currently doing. This is the replacement under Linux for the Unix command whodo:

root@ubuntu1604:~# w
15:27:17 up 1 day, 13:19, 1 user, load average: 0,10, 0,06, 0,06
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
trainee pts/0 10.0.2.2 lun.17 0.00s 0.11s 0.00s sshd: trainee [priv]

The JCPU time is the time used by all processes attached to the tty. It does not include past background jobs, but does include currently running background jobs.

The PCPU time is the time used by the current process, named in the what field.

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the w command to view the command line switches. [/stextbox]

The uname Command

This command prints system information to the standard output:

root@ubuntu1604:~# uname -a
Linux ubuntu1604 4.4.0-21-generic #37-Ubuntu SMP Mon Apr 18 18:33:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
root@ubuntu1604:~# uname -s
Linux
root@ubuntu1604:~# uname -n
ubuntu1604
root@ubuntu1604:~# uname -r
4.4.0-21-generic
root@ubuntu1604:~# uname -v
#37-Ubuntu SMP Mon Apr 18 18:33:37 UTC 2016
root@ubuntu1604:~# uname -m
x86_64
root@ubuntu1604:~# uname -p
x86_64
root@ubuntu1604:~# uname -i
x86_64
root@ubuntu1604:~# uname -o
GNU/Linux

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the uname command to view the command line switches. [/stextbox]

The du Command

This command summarizes disk usage of each file, recursively for directories:

root@ubuntu1604:~# du -sh /* 2>/dev/null
13M	/bin
100M	/boot
4,0K	/cdrom
0	/dev
13M	/etc
1,7M	/home
0	/initrd.img
0	/initrd.img.old
584M	/lib
4,0K	/lib64
16K	/lost+found
4,0K	/media
4,0K	/mnt
155M	/opt
0	/proc
6,1M	/root
4,1M	/run
13M	/sbin
4,0K	/snap
4,0K	/srv
0	/sys
72K	/tmp
2,9G	/usr
341M	/var
0	/vmlinuz
0	/vmlinuz.old

[stextbox id='black' image='null'] Important : Note the use of the 2>/dev/null redirection. This sends all eventual errors in the file descriptor 2 directly to /dev/null so that they do not appear in the output. File descriptors are covered in the The Command Line Interface unit. [/stextbox]

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the du command to view the command line switches. [/stextbox]

The clear Command

This command is used to clear the current screen of the terminal:

root@ubuntu1604:~# clear
root@ubuntu1604:~#

The exit Command

This command exits the current shell:

root@ubuntu1604:~# exit
logout
trainee@ubuntu1604:~$

Command Line Switches

[stextbox id='black' image='null'] To do : Use the help command with exit option to view the command line switches. [/stextbox]

The logout Command

This command logs out a user from a login shell writing the utmp and wtmp entries in the log files.

Command Line Switches

[stextbox id='black' image='null'] To do : Use the help command with logout option to view the command line switches. [/stextbox]

The sleep Command

This command pauses for a number seconds. The number is specified as the first argument.

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the sleep command to view the command line switches. [/stextbox]

The wall Command

The wall command displays a message, or the contents of a file, or otherwise its standard input, on the terminals of all currently logged in users.The command will wrap lines that are longer than 79 characters. Short lines are whitespace padded to have 79 characters. The command will always put a carriage return and new line at the end of each line. Only the superuser can write on the terminals of users who have chosen to deny messages or are using a program which automatically denies messages. Reading from a file is refused when the invoker is not superuser and the program is suid or sgid.

Start a second session as trainee via ssh on your VM. Return to your first session as root and type :

trainee@ubuntu1604:~$ sudo su -
[sudo] password for trainee: trainee
root@ubuntu1604:~# wall this is a message from root
Broadcast message from trainee@ubuntu1604 (pts/0) (Fri Oct 7 10:18:21 2016):
this is a message from root
root@ubuntu1604:~#

In the second session you should see the following message :

Broadcast message from trainee@ubuntu1604 (pts/0) (Fri Oct 7 10:18:21 2016):
this is a message from root

The seq Command

The seq command prints numbers from FIRST to LAST, in steps of INCREMENT:

seq [OPTION]… LAST

seq [OPTION]… FIRST LAST

seq [OPTION]… FIRST INCREMENT LAST

For example :

root@ubuntu1604:~# seq 10
1
2
3
4
5
6
7
8
9
10
root@ubuntu1604:~# seq 20 30
20
21
22
23
24
25
26
27
28
29
30
root@ubuntu1604:~# seq 20 10 90
20
30
40
50
60
70
80
90
root@ubuntu1604:~#

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the seq command to view the command line switches. [/stextbox]

The screen Command

Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells). Each virtual terminal provides the functions of a DEC VT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI X3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows moving text regions between windows.

The screen command is not installed by default under Ubuntu 16.04 LTS. Use the apt-get to install it:

root@ubuntu1604:~# which screen
root@ubuntu1604:~# apt-get install screen
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libpango1.0-0 libpangox-1.0-0
Use 'apt autoremove' to remove them.
Suggested packages:
iselect | screenie | byobu
The following NEW packages will be installed:
screen
0 upgraded, 1 newly installed, 0 to remove and 1 not upgraded.
Need to get 560 kB of archives.
After this operation, 972 kB of additional disk space will be used.
Get:1 http://fr.archive.ubuntu.com/ubuntu xenial/main amd64 screen amd64 4.3.1-2build1 [560 kB]
Fetched 560 kB in 0s (2 306 kB/s)
Selecting previously unselected package screen.
(Reading database ... 207619 files and directories currently installed.)
Preparing to unpack .../screen_4.3.1-2build1_amd64.deb ...
Unpacking screen (4.3.1-2build1) ...
Processing triggers for systemd (229-4ubuntu10) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for install-info (6.1.0.dfsg.1-5) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up screen (4.3.1-2build1) ...
Processing triggers for systemd (229-4ubuntu10) ...
Processing triggers for ureadahead (0.100.0-19) ...
root@ubuntu1604:~# which screen
/usr/bin/screen

Create a session with screen:

root@ubuntu1604:~# screen -S mysession

Now press the CTRL and A keys, release the A key and press the C key in order to create a second nested screen.

To return to the first screen, use the CTRL A A keys. This allows you to toggle between the last two screens used.

To see the status of all active screens, use the screen -ls command:

root@ubuntu1604:~# screen -ls
There is a screen on:
1745.mysession (07/10/2016 11:00:55) (Attached)
1 Socket in /var/run/screen/S-root

Now enter the following commands:

root@ubuntu1604:~# sleep 9999 &
[1] 9699
root@ubuntu1604:~# jobs
[1]+ Running sleep 9999 &
root@ubuntu1604:~#

In order to detach the current screen press the CTRL and A keys, release the A key and press the D key:

root@ubuntu1604:~# screen -S mysession
[detached from 1745.mysession]
root@ubuntu1604:~#

To re-attach the screen, execute the following command:

root@ubuntu1604:~# screen -r

Using the jobs command, check if the process created by the sleep command is still running:unit

root@ubuntu1604:~# jobs
[1]+ Running sleep 9999 &

To move forward or backwards between screens press the CTRL and A keys, release the A key and press the N key or press the CTRL and A keys, release the A key and press the P key.

Once again detach the current screen by pressing the CTRL A keys, releasing the A key and then pressing D:

root@ubuntu1604:~# screen -S mysession
[detached from 1745.mysession]
root@ubuntu1604:~#

Now create a new, non-nested screen:

root@ubuntu1604:~# screen -S mysession1

Use the screen -ls command to see what has happened:

root@ubuntu1604:~# screen -ls
There are screens on:
15800.mysession1 (07/10/2016 13:58:29) (Attached)
1745.mysession (07/10/2016 11:00:55) (Detached)
2 Sockets in /var/run/screen/S-root.

To re-attach a specific screen, reference it by it's number:

root@ubuntu1604:~# screen -r 1745

Finally, check out what has happened:

root@ubuntu1604:~# sleep 9999 &
[1] 9699
root@ubuntu1604:~# jobs
[1]+ Running sleep 9999 &
root@ubuntu1604:~# jobs
[1]+ Running sleep 9999 &
root@ubuntu1604:~# screen -ls
There are screens on:
15800.mysession1 (07/10/2016 13:58:29) (Attached)
1745.mysession (07/10/2016 11:00:55) (Attached)
2 Sockets in /var/run/screen/S-root.

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the screen command to view the command line switches. [/stextbox]

Switches and Arguments

Switches under Linux can either be short or long. Several differences are important to note.

Firstly short options are generally preceded by a single dash -, whilst long options are preceded by a double dash - -.

An example is the help option used with most commands:

-h

–help

Secondly, Linux short switches can be combined whereas long switches cannot be combined. For example ls -l -a -i can also be written as ls -lai, ls -lia or ls -ali:

root@ubuntu1604:~# ls -lai /tmp
total 40
390918 drwxrwxrwt 10 root root 4096 oct. 7 16:31 .
2 drwxr-xr-x 24 root root 4096 sept. 28 10:40 ..
521792 drwxrwxrwt 2 root root 4096 sept. 28 10:31 .font-unix
522570 drwxr-xr-x 2 root root 4096 sept. 28 10:34 hsperfdata_root
521777 drwxrwxrwt 2 root root 4096 sept. 28 10:31 .ICE-unix
521308 drwxr-xr-x 2 root root 4096 sept. 29 10:32 inode
395765 srw------- 1 root root 0 sept. 28 10:34 .java_pid2124
521306 drwx------ 3 root root 4096 sept. 29 10:19 systemd-private-cd33d40e1d3a4e08a9cde3de3603311e-systemd-timesyncd.service-bxC1MJ
521801 drwxrwxrwt 2 root root 4096 sept. 28 10:31 .Test-unix
521757 drwxrwxrwt 2 root root 4096 sept. 28 10:31 .X11-unix
521785 drwxrwxrwt 2 root root 4096 sept. 28 10:31 .XIM-unix
root@ubuntu1604:~# ls -ali /tmp
total 40
390918 drwxrwxrwt 10 root root 4096 oct. 7 16:31 .
2 drwxr-xr-x 24 root root 4096 sept. 28 10:40 ..
521792 drwxrwxrwt 2 root root 4096 sept. 28 10:31 .font-unix
522570 drwxr-xr-x 2 root root 4096 sept. 28 10:34 hsperfdata_root
521777 drwxrwxrwt 2 root root 4096 sept. 28 10:31 .ICE-unix
521308 drwxr-xr-x 2 root root 4096 sept. 29 10:32 inode
395765 srw------- 1 root root 0 sept. 28 10:34 .java_pid2124
521306 drwx------ 3 root root 4096 sept. 29 10:19 systemd-private-cd33d40e1d3a4e08a9cde3de3603311e-systemd-timesyncd.service-bxC1MJ
521801 drwxrwxrwt 2 root root 4096 sept. 28 10:31 .Test-unix
521757 drwxrwxrwt 2 root root 4096 sept. 28 10:31 .X11-unix
521785 drwxrwxrwt 2 root root 4096 sept. 28 10:31 .XIM-unix

However ls -l –all –inode cannot be written ls -l –allinode:

root@ubuntu1604:~# ls -l --all --inode /tmp
total 40
390918 drwxrwxrwt 10 root root 4096 oct. 7 16:31 .
2 drwxr-xr-x 24 root root 4096 sept. 28 10:40 ..
521792 drwxrwxrwt 2 root root 4096 sept. 28 10:31 .font-unix
522570 drwxr-xr-x 2 root root 4096 sept. 28 10:34 hsperfdata_root
521777 drwxrwxrwt 2 root root 4096 sept. 28 10:31 .ICE-unix
521308 drwxr-xr-x 2 root root 4096 sept. 29 10:32 inode
395765 srw------- 1 root root 0 sept. 28 10:34 .java_pid2124
521306 drwx------ 3 root root 4096 sept. 29 10:19 systemd-private-cd33d40e1d3a4e08a9cde3de3603311e-systemd-timesyncd.service-bxC1MJ
521801 drwxrwxrwt 2 root root 4096 sept. 28 10:31 .Test-unix
521757 drwxrwxrwt 2 root root 4096 sept. 28 10:31 .X11-unix
521785 drwxrwxrwt 2 root root 4096 sept. 28 10:31 .XIM-unix
root@ubuntu1604:~# ls -l --allinode /tmp
ls: unrecognized option '--allinode'
Try 'ls --help' for more information.

[stextbox id='black' image='null'] Important - You should not combine any short options that take an argument. [/stextbox]

Manipulating Text Files

Text files play a very important role under Linux. For example, almost all configuration files are simple text files and being able to manipulate them is of great importance. Manipulating text files is essentially achieved by using Regular Expressions. There are two types of Regular Expressions:

The IEEE POSIX Basic Regular Expressions (BRE) understood by the commands vi, grep, expr and sed,

The IEEE POSIX Extended Regular Expressions (ERE) understood by the commands egrep ( grep -E ) and awk.

Regular Expressions

Regular Expressions use Metacharacters. Certain are common to both BREs and EREs:

Metacharacter	Description
^string	Match lines beginning with string
string$	Match lines ending with string
\Metacharacter	Cancel any special effect associated with Metacharacter
[string]	Match any of the characters within string
[^string]	Exclude any of the characters in string
.	Match any character except when at the end of a line
character*	Match 0 or more occurrences of character
\<	Match string at the beginning of a word
\>	Match string at the end of a word

BREs

Certain Metacharacters are specific to BREs:

Metacharacter	Description
\{x,y\}	Match from x to y occurrences of the preceeding element
\{x\}	Match exactly x occurrences of the preceeding element
\{x,\}	Match x or more occurrences of the preceeding element
\(BRE)	Commit to memory the BRE
\1	Recall the first BRE committed to memory
\2, \3 …	Recall the second BRE committed to memory, recall the third BRE committed to memory …

EREs

Certain Metacharacters are specific to EREs:

Metacharacter	Description
{x,y}	Match from x to y occurrences of the preceeding element
{x}	Match exactly x occurrences of the preceeding element
{x,}
?	Matches 0 or 1 occurrence of the preceeding element
+	Matches 1 or more occurrence(s) of the preceeding element
|	Matches either the expression before or the expression after the operator
()	Combines the Reguler Expressions between the parentheses

Text-search Utilities

The grep Command

The grep command can be used to find lines containing a string of characters in a group of files. The -v or –invert-case option can be stipulated to find lines that do not contain the specified string.

The grep command is case sensitive. The -i or –ignore-case option can be specified in order to use grep in a non case sensitive search.

The grep command can also use BREs.

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the grep command to view the command line switches. [/stextbox]

The egrep Command

The egrep command is identical to the grep command when used with the -E switch. Both can use EREs.

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the egrep command to view the command line switches. [/stextbox]

The fgrep Command

The fgrep command is identical to the grep command when used with the -F switch. Both have no knowledge of Regular Expressions.

[stextbox id='black' image='null'] To do : Use the –help option of the fgrep command to view the command line switches. [/stextbox]

LAB #1 - Using grep, egrep and fgrep

Download the following file by clicking on it's title:

fenestrOS
fenestros
555-5555
f
.fenestros
.fe
£

Move the file to the /tmp directory:

root@ubuntu1604:~# mv /home/trainee/Downloads/greptest /tmp/greptest

Now use grep to search for lines containing at least one uppercase or lowercase letter:

root@ubuntu1604:~# grep '[a-zA-Z]' /tmp/greptest
fenestrOS
fenestros
f
.fenestros
.fe

Next use grep to search for lines containing at least one uppercase or lowercase letter or a number:

root@ubuntu1604:~# grep '[a-zA-Z0-9]' /tmp/greptest
fenestrOS
fenestros
555-5555
f
.fenestros
.fe

To search for the NNN-NNNN pattern where N is a number, use the following command:

root@ubuntu1604:~# grep '[0-9]\{3\}-[0-9]\{4\}' /tmp/greptest
555-5555

Lines containing just one character have that character both at the beginning (^) and at the end ($) of the line:

root@ubuntu1604:~# grep '^.$' /tmp/greptest
f
£

To search for a line containing a special character such as ., that character needs to be preceded by \:

root@ubuntu1604:~# grep '^\.' /tmp/greptest
.fenestros
.fe

[stextbox id='black' image='null'] Important - The grep command can also be used to search for a string in all the files within a specific directory as follows grep -rnw 'directory' -e “pattern”. You can also search only within certain files by specifying the files extensions: grep –include={*.doc,*.xls} -rnw 'directory' -e “pattern”. Finally you can exclude certain file extensions as follows: grep –exclude=*.doc -rnw 'directory' -e “pattern”. [/stextbox]

Download the following file by clicking on it's title:

# Starting comment
fenestrOS
fenestros
# Another comment
555-5555
f
.fenestros
.fe
£
# End comment

Move the file to the /tmp directory:

root@ubuntu1604:~# mv /home/trainee/Downloads/greptest /tmp/greptest

Now use the grep command with the -E switch to remove all the comments and empty lines:

root@ubuntu1604:~# grep -E -v '^(#|$)' /tmp/greptest
fenestrOS
fenestros
555-5555
f
.fenestros
.fe
£

[stextbox id='black' image='null'] The expression '^(#|$)' matches all lines beginning with the # character OR all lines with zero characters between the start and the end of the line. [/stextbox]

Now use the egrep command to do the same thing, this time redirecting the output to the file /tmp/greptest1:

root@ubuntu1604:~# egrep -v '^(#|$)' /tmp/greptest > /tmp/greptest1
root@ubuntu1604:~# cat /tmp/greptest1
fenestrOS
fenestros
555-5555
f
.fenestros
.fe
£

[stextbox id='black' image='null'] Important: The above command is very useful when you want to quickly ascertain which directives are active in a very long configuration file. [/stextbox]

Download the following file by clicking on it's title:

# Starting comment ^ This line will be used to demonstrate the use of fgrep fenestrOS fenestros # Another comment 555-5555 f .fenestros .fe £ # End comment

Move the file to the /tmp directory:

root@ubuntu1604:~# mv /home/trainee/Downloads/greptest /tmp/greptest

Now use fgrep to match the line starting with the ^ character:

root@ubuntu1604:~# fgrep '^' /tmp/greptest
^ This line will be used to demonstrate the use of fgrep

Compare the above output to that when using the grep command:

root@ubuntu1604:~# grep '^' /tmp/greptest
# Starting comment
^ This line will be used to demonstrate the use of fgrep
fenestrOS
fenestros
# Another comment
555-5555
f
.fenestros
.fe
£
# End comment

As you can see, grep matched every line that had a beginning. In order to get the same result as the grep command, you need to use the following command:

root@ubuntu1604:~# grep '^\^' /tmp/greptest
^ This line will be used to demonstrate the use of fgrep

The Stream EDitor SED

sed is an abbreviation of Stream EDitor and is a non-interactive text editor. sed's basic syntax is as follows:

sed [address] command [arguments] file

The specified commands are applied to each line in the file unless an address is specified. Sed prints all results to standard output and does not modify the source file. The address therefore specifies which lines are concerned by the command.

sed's addresses are as follows:

address	Matching lines
x	Line number x
$	The last line of the file
/BRE/	Lines matching the specified BRE
x,y	From line x to line y
/ERb1/, /ERb2/	All lines from the first line that matches the first BRE to the first line that matches the second BRE

sed's commands are as follows:

command	Description
d	Do not show the matching line(s) on standard output
p	Show the matching line(s) on standard output
s	Do a substitution upon match
w	Write the matching line(s) to a file
=	Print the matching line's number
!	Exclude the line(s) matching the address

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the fgrep command to view the command line switches. [/stextbox]

LAB #2 - Using sed

Start by displaying the contents of the file /etc/services whilst inhibiting the display of the first 10 lines:

root@ubuntu1604:~# sed '1,10d' /etc/services | more
# If you need a huge list of used numbers please install the nmap package.
tcpmux	1/tcp	# TCP port service multiplexer
echo	7/tcp
echo	7/udp
discard	9/tcp	sink null
discard	9/udp	sink null
systat	11/tcp	users
daytime	13/tcp
daytime	13/udp
netstat	15/tcp
qotd	17/tcp	quote
msp	18/tcp	# message send protocol
msp	18/udp
chargen	19/tcp	ttytst source
chargen	19/udp	ttytst source
ftp-data	20/tcp
ftp	21/tcp
fsp	21/udp	fspd
ssh	22/tcp	# SSH Remote Login Protocol
ssh	22/udp
telnet	23/tcp
smtp	25/tcp	mail
--More--

Now display the same file without any commented lines:

root@ubuntu1604:~# sed '/^#/d' /etc/services | more
tcpmux	1/tcp	# TCP port service multiplexer
echo	7/tcp
echo	7/udp
discard	9/tcp	sink null
discard	9/udp	sink null
systat	11/tcp	users
daytime	13/tcp
daytime	13/udp
netstat	15/tcp
qotd	17/tcp	quote
msp	18/tcp	# message send protocol
msp	18/udp
chargen	19/tcp	ttytst source
chargen	19/udp	ttytst source
ftp-data	20/tcp
ftp	21/tcp
fsp	21/udp	fspd
ssh	22/tcp	# SSH Remote Login Protocol
ssh	22/udp
telnet	23/tcp
smtp	25/tcp	mail
time	37/tcp	timserver
--More--

[stextbox id='black' image='null'] Important: Note that the BRE is preceeded and followed by the / character. [/stextbox]

Continue by trying to just display the first two lines of /etc/passwd:

root@ubuntu1604:~# sed '1,2p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
...

[stextbox id='black' image='null'] Important: As you can see in the above output, the command used displays not only the first two lines but also the entire file. As a result the first two lines are displayed twice. [/stextbox]

To force sed to only display the lines you specify, use the -n switch:

root@ubuntu1604:~# sed -n '1,2p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin

Now you want to use sed to strip out the comments from /etc/services and save the result to /tmp/sedtest without displaying anything on standard output:

root@ubuntu1604:~# sed -n '/^#/!w /tmp/sedtest' /etc/services
root@ubuntu1604:~# more /tmp/sedtest
tcpmux	1/tcp	# TCP port service multiplexer
echo	7/tcp
echo	7/udp
discard	9/tcp	sink null
discard	9/udp	sink null
systat	11/tcp	users
daytime	13/tcp
daytime	13/udp
netstat	15/tcp
qotd	17/tcp	quote
msp	18/tcp	# message send protocol
msp	18/udp
chargen	19/tcp	ttytst source
chargen	19/udp	ttytst source
ftp-data	20/tcp
ftp	21/tcp
fsp	21/udp	fspd
ssh	22/tcp	# SSH Remote Login Protocol
ssh	22/udp
telnet	23/tcp
smtp	25/tcp	mail
time	37/tcp	timserver
--More--(2%)

[stextbox id='black' image='null'] Important: In the above command, we start by matching all lines in the /etc/services file that start with a #. We then tell sed to write all non-matching lines to the file /tmp/sedtest. [/stextbox]

Finally, create a file containing user1,user2,user3. Replace the commas by spaces:

root@ubuntu1604:~# echo "user1,user2,user3" > /tmp/sedtest1
root@ubuntu1604:~# cat /tmp/sedtest1 | sed 's/,/ /g'
user1 user2 user3

[stextbox id='black' image='null'] Important: The above sed command has the following format s/what is to be replaced (character, string or BRE)/replacement/g. The use of the g character forces sed to replace all occurences that match. If g is not stipulated, only the first matching occurence is replaced. [/stextbox]

The Text Processor AWK

Presentation

The awk command acts as a filter and uses the following syntax:

awk [-F seperator] '[condition] {action}' [file]

Field Separation

A file or a text stream is treated by awk as a sequence of records. By default each line is a record. Awk analyzes each record, separating that record into fields and then storing the record and fields in variables:

$0 contains the record,

$1 contains the first field,

$2 contains the second field,

e.t.c.

Awk interprets a space as a separator between fields unless a different separator is specified with the -F option.

Awk then checks if the condition is met for each record and if so, executes the action.

For example, the following command takes the standard output of ls -l and prints fields 8, 3 and 4 to standard output:

root@ubuntu1604:~# ls -l | awk '{print $8 $3 $4}'
10:40rootroot
2016rootroot
2016rootroot

Since there is no condition, the action is applied to every record.

To make the output easier to read, you can include spaces between each field:

root@ubuntu1604:~# ls -l | awk '{print $8 " " $3 " " $4}'
10:40 root root
2016 root root
2016 root root

Conditions

A regular expression applied to a record

Format:

/regular expression/ {action}

Exemple:

/hello/ {print $0}

A regular expression applied to a field

Format:

$n ~/regular expression/ {action}

$n!~/regular expression/ {action}

Examples:

$1 ~/hello/ {print $0}

$1!~/hello/ {print $0}

Comparisons

Format:

$n operator criteria {action}

Example:

$1 > 20 {print $0}

Operators

Operator	Condition
<	Less than
⇐	Less than or equal to
==	Equal to
!=	Different
>	Greater than
>=	Greater than or equal to

Logical Operators

Format:

test1 logical operator test2 {action}

Example:

$1 ~/hello/ && $2 > 20 {print $0}

Operators

Operator	Condition
||	OR
&&	AND
!	NO

Built-in Variables

Format:

expression1, expression2 {instruction}

Example:

NR==7, NR==10 {print $0}

Variables

Variable	Description
NR	Total number of records
NF	Total number of fields
FILENAME	Name of current input file
FS	The field separator, by default a space or tab
RS	The record separator, by default newline
OFS	Output field separator, by default a space
ORS	Output record separator, by default newline
OFMT	Numeric output format, by default“%.6g”

Awk Scripts

To combine several clauses composed of conditions and actions in the same statement, it is advisable to create an awk script. Awk scripts are comprised of three sections:

BEGIN

This section is executed once, prior to executing the body of the script

BODY

This sections contains the clauses to be applied to each line

END

This section is executed once, after executing the body of the script

For example:

root@ubuntu1604:~# cat > awkscript
BEGIN {
print "List of the currently mounted file systems"}
{print $0}
END {
print "=========================================="}
[^D]

Now apply the awk script to /etc/fstab :

root@ubuntu1604:~# awk -f awkscript /etc/fstab
List of the currently mounted file systems
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda1 during installation
UUID=c27fce7f-cc8a-4c6f-b19b-d929a4d570f2 / ext4 errors=remount-ro 0 1
# swap was on /dev/sda5 during installation
UUID=68f67549-63f1-4833-b792-3566455bbe95 none swap sw 0 0
==========================================

[stextbox id='black' image='null'] Important: Note the use of the -f switch which instructs awk to use the script. [/stextbox]

The printf function

The integrated function printf is used to format output and has the following syntax:

printf ("string",expression1,expression2,...,expressionn)

string contains as many formats as there are expressions.

Examples of formats commonly used are:

Format	Description
%30s	Displays a right-justified string of 30 characters
%-30s	Displays a left-justified string of 30 characters
%4d	Displays a right-justified decimal number of 4 digits
%-4d	Displays a left-justified decimal number of 4 digits

Control Statements

awk can use the following control statements:

if

if condition {
command
command
...
}
else {
command
command
...
}

or:

if condition
command
else
command

for

for variable in list {
command
command
...
}

or:

for variable in list
command

or in the case of a table:

for key in table {
print key , table[key]
}

while

while condition {
command
command
...
}

do-while

do {
command
command
...
} while condition

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the awk command to view the command line switches. [/stextbox]

LAB #3 - Using awk

Download the file sales.txt by clicking on the title below:

# FenestrOs.com # Annual sales by French department # 83 Desktops§100 Portables§50 Servers§21 Ipads§4 # 06 Desktops§99 Portables§60 Servers§8 Ipads§16 # 13 Desktops§130 Portables§65 Servers§12 Ipads§56

Now download the awk script sales.awk by clicking on the title below:

# BEGIN BEGIN { FS="§" } # TABLE $1 !~ /^#/ && $1 !~ /^$/ { sales[$1]+=$2 } # END END { for (pc in sales) printf("PC Type : %s \t Sales (06+13+83) : %10d\n",pc,sales[pc]); }

This script contains 13 lines. The purpose of this script is to calculate the total number of computers sold in the three French departments from the data present in the sales file:

1	# BEGIN
2	BEGIN {
3	FS="§"
4	}
5	# TABLE
6	$1 !~ /^#/ && $1 !~ /^$/ {
7	sales[$1]+=$2
8	}
9	# END
10	END {
11	for (pc in sales)
12	printf("PC Type : %s \t Sales (06+13+83) : %10d\n",pc,sales[pc]);
13	}

It is important that you understand the key lines in the above script:

Line 3,

Defines a new field seperator in a BEGIN section.

Line 6,

Discards all commented and empty lines.

Line 7,

The table's key is $1, in other words the different types of computers. Against each key, the number of each type of computer sold is stored in $2. The += characters indicate that the value stored in $2 is incremental.

Line 12,

Uses printf to format the output of each line in the table.

Now execute the script and check the output is correct:

root@ubuntu1604:~# awk -f /home/trainee/Downloads/sales.awk /home/trainee/Downloads/sales.txt
PC Type : Servers	Sales (06+13+83) : 41
PC Type : Portables	Sales (06+13+83) : 175
PC Type : Ipads	Sales (06+13+83) : 76
PC Type : Desktops	Sales (06+13+83) : 329

Other Useful Commands

The expand Command

The expand command converts tabulations in a file to spaces and prints the results to STDOUT. With no file as an argument or with the - character as an argument, the command takes it's input from STDIN.

Download the following file:

un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq

Move the file to the /root folder:

root@ubuntu1604:~# mv /home/trainee/Downloads/expand.txt /root/expand

Use the cat command to view the contents of the file:

root@ubuntu1604:~# cat expand
un	deux	trois	quatre	cinq
un	deux	trois	quatre	cinq
un	deux	trois	quatre	cinq
un	deux	trois	quatre	cinq
un	deux	trois	quatre	cinq
un	deux	trois	quatre	cinq
un	deux	trois	quatre	cinq
un	deux	trois	quatre	cinq
un	deux	trois	quatre	cinq
un	deux	trois	quatre	cinq

Now use the -vet switches of the cat command to view the non-printable characters:

root@ubuntu1604:~# cat -vet expand
un^Ideux^Itrois^Iquatre^Icinq$
un^Ideux^Itrois^Iquatre^Icinq$
un^Ideux^Itrois^Iquatre^Icinq$
un^Ideux^Itrois^Iquatre^Icinq$
un^Ideux^Itrois^Iquatre^Icinq$
un^Ideux^Itrois^Iquatre^Icinq$
un^Ideux^Itrois^Iquatre^Icinq$
un^Ideux^Itrois^Iquatre^Icinq$
un^Ideux^Itrois^Iquatre^Icinq$
un^Ideux^Itrois^Iquatre^Icinq$

[stextbox id='black' image='null'] Important : As you can see the tabulations are shown as ^I and the end o each line as a $. [/stextbox]

Now use the expand command to convert the tabulations into spaces and send the result to the expand1 file:

root@ubuntu1604:~# expand expand > expand1

View the resulting expand1 file with the cat command and the -vet switches:

root@ubuntu1604:~# cat -vet expand1
un deux trois quatre cinq$
un deux trois quatre cinq$
un deux trois quatre cinq$
un deux trois quatre cinq$
un deux trois quatre cinq$
un deux trois quatre cinq$
un deux trois quatre cinq$
un deux trois quatre cinq$
un deux trois quatre cinq$
un deux trois quatre cinq$

[stextbox id='black' image='null'] Important : As you can see, the tabulations have been changed into spaces. [/stextbox]

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the expand command to view the command line switches. [/stextbox]

La Commande unexpand

The expand command converts spaces in a file to tabulations and prints the results to STDOUT. With no file as an argument or with the - character as an argument, the command takes it's input from STDIN.

Now use the expand command to convert the spaces in the expand1 file into tabulations and send the result to the expand2 file:

root@ubuntu1604:~# cat -vet expand1
un deux trois quatre cinq$
un deux trois quatre cinq$
un deux trois quatre cinq$
un deux trois quatre cinq$
un deux trois quatre cinq$
un deux trois quatre cinq$
un deux trois quatre cinq$
un deux trois quatre cinq$
un deux trois quatre cinq$
un deux trois quatre cinq$
root@ubuntu1604:~# unexpand -a expand1 > expand2
root@ubuntu1604:~# cat -vet expand2
un^Ideux^Itrois^Iquatre^Icinq$
un^Ideux^Itrois^Iquatre^Icinq$
un^Ideux^Itrois^Iquatre^Icinq$
un^Ideux^Itrois^Iquatre^Icinq$
un^Ideux^Itrois^Iquatre^Icinq$
un^Ideux^Itrois^Iquatre^Icinq$
un^Ideux^Itrois^Iquatre^Icinq$
un^Ideux^Itrois^Iquatre^Icinq$
un^Ideux^Itrois^Iquatre^Icinq$
un^Ideux^Itrois^Iquatre^Icinq$

[stextbox id='black' image='null'] Important : Note that the spaces have been replaced by tabulations. [/stextbox]

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the unexpand command to view the command line switches. [/stextbox]

The cut command

The cut command splits each line of a file into columns starting with column 1. Each column contains one character. The command can also be used to split lines into fields where the default seperator is a tablulation. The default seperator can be changed by using the -d switch.

Select the first 7 columns of the /etc/passwd file:

root@ubuntu1604:~# cut -c1-7 /etc/passwd
root:x:
daemon:
bin:x:2
sys:x:3
sync:x:
games:x
man:x:6
lp:x:7:
mail:x:
news:x:
uucp:x:
proxy:x
www-dat
backup:
list:x:
irc:x:3
gnats:x
nobody:
systemd
systemd
systemd
systemd
syslog:
_apt:x:
message
uuidd:x
lightdm
whoopsi
avahi-a
avahi:x
dnsmasq
colord:
speech-
hplip:x
kernoop
pulse:x
rtkit:x
saned:x
usbmux:
trainee
sshd:x:

In order to select columns 1 to 5, columns 10 to 15 and columns 30 and higher, us the following command:

root@ubuntu1604:~# cut -c1-5,10-15,30- /etc/passwd
root:0:rootsh
daemo1:1:da:/usr/sbin/nologin
bin:x:bin:/nologin
sys:x:sys:/nologin
sync:65534:/sync
games:60:ga:/usr/sbin/nologin
man:x2:man::/usr/sbin/nologin
lp:x:lp:/vasr/sbin/nologin
mail:8:mailr/sbin/nologin
news:9:newsws:/usr/sbin/nologin
uucp::10:uuuucp:/usr/sbin/nologin
proxy3:13:p/sbin/nologin
www-dx:33:3r/www:/usr/sbin/nologin
backu34:34:ckups:/usr/sbin/nologin
list::38:Maager:/var/list:/usr/sbin/nologin
irc:x39:ircd:/usr/sbin/nologin
gnats1:41:Gting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobod65534:nonexistent:/usr/sbin/nologin
systeimesynstemd Time Synchronization,,,:/run/systemd:/bin/false
systeetworktemd Network Management,,,:/run/systemd/netif:/bin/false
systeesolvetemd Resolver,,,:/run/systemd/resolve:/bin/false
systeus-proystemd Bus Proxy,,,:/run/systemd:/bin/false
syslo104:10g:/bin/false
_apt:5:6553t:/bin/false
messas:x:10n/dbus:/bin/false
uuidd07:111bin/false
light:108:1ay Manager:/var/lib/lightdm:/bin/false
whoopx:109:ent:/bin/false
avahioipd:x autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false
avahi11:120emon,,,:/var/run/avahi-daemon:/bin/false
dnsma:112:6,:/var/lib/misc:/bin/false
color113:12r management daemon,,,:/var/lib/colord:/bin/false
speecspatcheech Dispatcher,,,:/var/run/speech-dispatcher:/bin/false
hplip15:7:Her,,,:/var/run/hplip:/bin/false
kernox:116:ops Tracking Daemon,,,:/:/bin/false
pulse17:124emon,,,:/var/run/pulse:/bin/false
rtkit18:126,:/proc:/bin/false
saned19:127ed:/bin/false
usbmu120:46,,,:/var/lib/usbmux:/bin/false
train:1000:,:/home/trainee:/bin/bash
sshd:1:6553hd:/usr/sbin/nologin

In order to select the 2nd, 4th and 6th column, use the following command:

root@ubuntu1604:~# cut -d: -f2,4,6 /etc/passwd
x:0:/root
x:1:/usr/sbin
x:2:/bin
x:3:/dev
x:65534:/bin
x:60:/usr/games
x:12:/var/cache/man
x:7:/var/spool/lpd
x:8:/var/mail
x:9:/var/spool/news
x:10:/var/spool/uucp
x:13:/bin
x:33:/var/www
x:34:/var/backups
x:38:/var/list
x:39:/var/run/ircd
x:41:/var/lib/gnats
x:65534:/nonexistent
x:102:/run/systemd
x:103:/run/systemd/netif
x:104:/run/systemd/resolve
x:105:/run/systemd
x:108:/home/syslog
x:65534:/nonexistent
x:110:/var/run/dbus
x:111:/run/uuidd
x:114:/var/lib/lightdm
x:116:/nonexistent
x:119:/var/lib/avahi-autoipd
x:120:/var/run/avahi-daemon
x:65534:/var/lib/misc
x:123:/var/lib/colord
x:29:/var/run/speech-dispatcher
x:7:/var/run/hplip
x:65534:/
x:124:/var/run/pulse
x:126:/proc
x:127:/var/lib/saned
x:46:/var/lib/usbmux
x:1000:/home/trainee
x:65534:/var/run/sshd

[stextbox id='black' image='null'] Important: Note the use of the -d switch to change the default seperator. [/stextbox]

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the cut command to view the command line switches. [/stextbox]

The uniq Command

The following command is used to extract the Primary Group GIDs from the /etc/passwd file:

root@ubuntu1604:~# cut -d: -f4 /etc/passwd | sort -n | uniq
0
1
2
3
7
8
9
10
12
13
29
33
34
38
39
41
46
60
102
103
104
105
108
110
111
114
116
119
120
123
124
126
127
1000
65534

[stextbox id='black' image='null'] Important: Note the use of the uniq command to remove duplicates from the list. [/stextbox]

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the uniq command to view the command line switches. [/stextbox]

The tr Command

The tr command is used to substitute certain characters by other characters. This command only accepts data from standard input (hence the pipe):

root@ubuntu1604:~# cat /etc/passwd | tr "[a-z]" "[A-Z]"
ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
DAEMON:X:1:1:DAEMON:/USR/SBIN:/USR/SBIN/NOLOGIN
BIN:X:2:2:BIN:/BIN:/USR/SBIN/NOLOGIN
SYS:X:3:3:SYS:/DEV:/USR/SBIN/NOLOGIN
SYNC:X:4:65534:SYNC:/BIN:/BIN/SYNC
GAMES:X:5:60:GAMES:/USR/GAMES:/USR/SBIN/NOLOGIN
MAN:X:6:12:MAN:/VAR/CACHE/MAN:/USR/SBIN/NOLOGIN
LP:X:7:7:LP:/VAR/SPOOL/LPD:/USR/SBIN/NOLOGIN
MAIL:X:8:8:MAIL:/VAR/MAIL:/USR/SBIN/NOLOGIN
NEWS:X:9:9:NEWS:/VAR/SPOOL/NEWS:/USR/SBIN/NOLOGIN
UUCP:X:10:10:UUCP:/VAR/SPOOL/UUCP:/USR/SBIN/NOLOGIN
PROXY:X:13:13:PROXY:/BIN:/USR/SBIN/NOLOGIN
WWW-DATA:X:33:33:WWW-DATA:/VAR/WWW:/USR/SBIN/NOLOGIN
BACKUP:X:34:34:BACKUP:/VAR/BACKUPS:/USR/SBIN/NOLOGIN
LIST:X:38:38:MAILING LIST MANAGER:/VAR/LIST:/USR/SBIN/NOLOGIN
IRC:X:39:39:IRCD:/VAR/RUN/IRCD:/USR/SBIN/NOLOGIN
GNATS:X:41:41:GNATS BUG-REPORTING SYSTEM (ADMIN):/VAR/LIB/GNATS:/USR/SBIN/NOLOGIN
NOBODY:X:65534:65534:NOBODY:/NONEXISTENT:/USR/SBIN/NOLOGIN
SYSTEMD-TIMESYNC:X:100:102:SYSTEMD TIME SYNCHRONIZATION,,,:/RUN/SYSTEMD:/BIN/FALSE
SYSTEMD-NETWORK:X:101:103:SYSTEMD NETWORK MANAGEMENT,,,:/RUN/SYSTEMD/NETIF:/BIN/FALSE
SYSTEMD-RESOLVE:X:102:104:SYSTEMD RESOLVER,,,:/RUN/SYSTEMD/RESOLVE:/BIN/FALSE
SYSTEMD-BUS-PROXY:X:103:105:SYSTEMD BUS PROXY,,,:/RUN/SYSTEMD:/BIN/FALSE
SYSLOG:X:104:108::/HOME/SYSLOG:/BIN/FALSE
_APT:X:105:65534::/NONEXISTENT:/BIN/FALSE
MESSAGEBUS:X:106:110::/VAR/RUN/DBUS:/BIN/FALSE
UUIDD:X:107:111::/RUN/UUIDD:/BIN/FALSE
LIGHTDM:X:108:114:LIGHT DISPLAY MANAGER:/VAR/LIB/LIGHTDM:/BIN/FALSE
WHOOPSIE:X:109:116::/NONEXISTENT:/BIN/FALSE
AVAHI-AUTOIPD:X:110:119:AVAHI AUTOIP DAEMON,,,:/VAR/LIB/AVAHI-AUTOIPD:/BIN/FALSE
AVAHI:X:111:120:AVAHI MDNS DAEMON,,,:/VAR/RUN/AVAHI-DAEMON:/BIN/FALSE
DNSMASQ:X:112:65534:DNSMASQ,,,:/VAR/LIB/MISC:/BIN/FALSE
COLORD:X:113:123:COLORD COLOUR MANAGEMENT DAEMON,,,:/VAR/LIB/COLORD:/BIN/FALSE
SPEECH-DISPATCHER:X:114:29:SPEECH DISPATCHER,,,:/VAR/RUN/SPEECH-DISPATCHER:/BIN/FALSE
HPLIP:X:115:7:HPLIP SYSTEM USER,,,:/VAR/RUN/HPLIP:/BIN/FALSE
KERNOOPS:X:116:65534:KERNEL OOPS TRACKING DAEMON,,,:/:/BIN/FALSE
PULSE:X:117:124:PULSEAUDIO DAEMON,,,:/VAR/RUN/PULSE:/BIN/FALSE
RTKIT:X:118:126:REALTIMEKIT,,,:/PROC:/BIN/FALSE
SANED:X:119:127::/VAR/LIB/SANED:/BIN/FALSE
USBMUX:X:120:46:USBMUX DAEMON,,,:/VAR/LIB/USBMUX:/BIN/FALSE
TRAINEE:X:1000:1000:TRAINEE,,,:/HOME/TRAINEE:/BIN/BASH
SSHD:X:121:65534::/VAR/RUN/SSHD:/USR/SBIN/NOLOGIN

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the tr command to view the command line switches. [/stextbox]

The paste Command

The paste command concatenates lines from n files. For example:

root@ubuntu1604:~# paste -d: /etc/passwd /etc/shadow
root:x:0:0:root:/root:/bin/bash:root:!:16924:0:99999:7:::
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin:daemon:*:16911:0:99999:7:::
bin:x:2:2:bin:/bin:/usr/sbin/nologin:bin:*:16911:0:99999:7:::
sys:x:3:3:sys:/dev:/usr/sbin/nologin:sys:*:16911:0:99999:7:::
sync:x:4:65534:sync:/bin:/bin/sync:sync:*:16911:0:99999:7:::
games:x:5:60:games:/usr/games:/usr/sbin/nologin:games:*:16911:0:99999:7:::
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin:man:*:16911:0:99999:7:::
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin:lp:*:16911:0:99999:7:::
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin:mail:*:16911:0:99999:7:::
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin:news:*:16911:0:99999:7:::
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin:uucp:*:16911:0:99999:7:::
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin:proxy:*:16911:0:99999:7:::
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin:www-data:*:16911:0:99999:7:::
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin:backup:*:16911:0:99999:7:::
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin:list:*:16911:0:99999:7:::
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin:irc:*:16911:0:99999:7:::
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin:gnats:*:16911:0:99999:7:::
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin:nobody:*:16911:0:99999:7:::
systemd-timesync:x:100:102:systemd Time Synchronization,,,:/run/systemd:/bin/false:systemd-timesync:*:16911:0:99999:7:::
systemd-network:x:101:103:systemd Network Management,,,:/run/systemd/netif:/bin/false:systemd-network:*:16911:0:99999:7:::
systemd-resolve:x:102:104:systemd Resolver,,,:/run/systemd/resolve:/bin/false:systemd-resolve:*:16911:0:99999:7:::
systemd-bus-proxy:x:103:105:systemd Bus Proxy,,,:/run/systemd:/bin/false:systemd-bus-proxy:*:16911:0:99999:7:::
syslog:x:104:108::/home/syslog:/bin/false:syslog:*:16911:0:99999:7:::
_apt:x:105:65534::/nonexistent:/bin/false:_apt:*:16911:0:99999:7:::
messagebus:x:106:110::/var/run/dbus:/bin/false:messagebus:*:16911:0:99999:7:::
uuidd:x:107:111::/run/uuidd:/bin/false:uuidd:*:16911:0:99999:7:::
lightdm:x:108:114:Light Display Manager:/var/lib/lightdm:/bin/false:lightdm:*:16911:0:99999:7:::
whoopsie:x:109:116::/nonexistent:/bin/false:whoopsie:*:16911:0:99999:7:::
avahi-autoipd:x:110:119:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false:avahi-autoipd:*:16911:0:99999:7:::
avahi:x:111:120:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false:avahi:*:16911:0:99999:7:::
dnsmasq:x:112:65534:dnsmasq,,,:/var/lib/misc:/bin/false:dnsmasq:*:16911:0:99999:7:::
colord:x:113:123:colord colour management daemon,,,:/var/lib/colord:/bin/false:colord:*:16911:0:99999:7:::
speech-dispatcher:x:114:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/false:speech-dispatcher:!:16911:0:99999:7:::
hplip:x:115:7:HPLIP system user,,,:/var/run/hplip:/bin/false:hplip:*:16911:0:99999:7:::
kernoops:x:116:65534:Kernel Oops Tracking Daemon,,,:/:/bin/false:kernoops:*:16911:0:99999:7:::
pulse:x:117:124:PulseAudio daemon,,,:/var/run/pulse:/bin/false:pulse:*:16911:0:99999:7:::
rtkit:x:118:126:RealtimeKit,,,:/proc:/bin/false:rtkit:*:16911:0:99999:7:::
saned:x:119:127::/var/lib/saned:/bin/false:saned:*:16911:0:99999:7:::
usbmux:x:120:46:usbmux daemon,,,:/var/lib/usbmux:/bin/false:usbmux:*:16911:0:99999:7:::
trainee:x:1000:1000:trainee,,,:/home/trainee:/bin/bash:trainee:$6$cloNcVp0$4BFXvmH./lvuhem.pWND1.XgChp/BuT4tHukwvFR9ykdQM1Yt.WbMO60lHdUR/E6D2bqUJ/n.BwX4gCMle/671:16924:0:99999:7:::
sshd:x:121:65534::/var/run/sshd:/usr/sbin/nologin:sshd:*:16924:0:99999:7:::

[stextbox id='black' image='null'] Important: Note that you need to become root to be able to execute this command. The reason is that normal users do not have the right to read the /etc/shadow file. [/stextbox]

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the paste command to view the command line switches. [/stextbox]

The split Command

The split command is used to divide a large file into smaller segments. Create an empty 250 MB file as follows:

root@ubuntu1604:~# dd if=/dev/zero of=/file bs=1024k count=250
250+0 records in
250+0 records out
262144000 bytes (262 MB, 250 MiB) copied, 2,57217 s, 102 MB/s

Now use the split command to divide the file into 5 smaller files each of 50:

root@ubuntu1604:~# split -b 50m /file filepart
root@ubuntu1604:~# ls -l | grep filepart
-rw-r--r-- 1 root root 52428800 déc. 17 11:31 filepartaa
-rw-r--r-- 1 root root 52428800 déc. 17 11:31 filepartab
-rw-r--r-- 1 root root 52428800 déc. 17 11:31 filepartac
-rw-r--r-- 1 root root 52428800 déc. 17 11:31 filepartad
-rw-r--r-- 1 root root 52428800 déc. 17 11:31 filepartae

[stextbox id='black' image='null'] Important: Note that the 5 files were created in the current working directory. [/stextbox]

You can re-construct the original file by using the cat command:

root@ubuntu1604:~# cat fileparta* > newfile
root@ubuntu1604:~# ls -l | grep newf
-rw-r--r-- 1 root root 262144000 déc. 17 11:32 newfile

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the split command to view the command line switches. [/stextbox]

The diff Command

The diff command compares two fles and indicates what changes need to be made to the first file in order that it be identical to the second file.

Copy the /etc/passwd file to the /root directory:

root@ubuntu1604:~# cp /etc/passwd /root

Edit the */root/passwd file as shown:

...
trainee10:x:1000:1000:trainee:/home/trainee:/bin/bash
...

Delete the lp entry and add the following line to the end of /root/passwd:

...
Linux is great!

Now compare the two files:

root@ubuntu1604:~# diff /etc/passwd /root/passwd
8d7
< lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
40c39
< trainee:x:1000:1000:trainee,,,:/home/trainee:/bin/bash
---
> trainee10:x:1000:1000:trainee,,,:/home/trainee:/bin/bash
41a41
> Linux is great!

In this output you will notice the < and > characters. The first makes reference to the first file, /etc/passwd, whist the second makes reference to the second file, /root/passwd.

The output 8d7 means that line 8 needs to be deleted in /etc/passwd because it is missing in /root/passwd.

The output 40c39 means that at line 40 in /etc/passwd needs to be modified in order to be the same as line 39 in /root/passwd.

The output 41a41 means that at line 41 in /root/passwd line 41 in /etc/passwd needs to be added because it is missing.

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the diff command to view the command line switches. [/stextbox]

The cmp Command

The cmp command compares two files character by character. By default, the command stops after finding the first difference:

root@ubuntu1604:~# cmp /root/passwd /etc/passwd
/root/passwd /etc/passwd differ: byte 286, line 8

The -l switch shows all of the differences in a three column format:

root@ubuntu1604:~# cmp -l /root/passwd /etc/passwd | more
cmp: EOF on /root/passwd
286 155 154
287 141 160
288 151 72
289 154 170
291 170 67
293 70 67
295 70 154
296 72 160
297 155 72
298 141 57
299 151 166
300 154 141
301 72 162
303 166 163
304 141 160
305 162 157
306 57 157
307 155 154
308 141 57
309 151 154
310 154 160
311 72 144
312 57 72
--More--

The first column represents the character number, the second column represents the ASCII octal value of the character in the /root/passwd file and the third column represents the ASCII octal value of the character in the /etc/passwd file.

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the cmp command to view the command line switches. [/stextbox]

The patch Command

The patch command is used to apply modifications contained within a patch file to an older version of a file so that it becomes the newer version of the file.

If you recall, you made some changes to the original greptest file that you downloaded:

root@ubuntu1604:~# cat /tmp/greptest
# Starting comment
^ This line will be used to demonstrate the use of fgrep
fenestrOS
fenestros
# Another comment
555-5555
f
.fenestros
.fe
£
# End comment

You also used egrep to remove all the comments and empty lines and save the result to /tmp/greptest1:

root@ubuntu1604:~# cat /tmp/greptest1
fenestrOS
fenestros
555-5555
f
.fenestros
.fe
£

Now create a patch file containing the modifications that need to be applied to /tmp/greptest in order for it to be identical to /tmp/greptest1:

root@ubuntu1604:~# cd /tmp
root@ubuntu1604:/tmp# diff -u greptest greptest1 > greptest.patch

A look at the patch file shows the chnages that need to be made to the greptest file:

root@ubuntu1604:/tmp# cat /tmp/greptest.patch
--- greptest	2016-12-09 15:47:38.836983792 +0100
+++ greptest1	2016-12-09 15:46:15.976860968 +0100
@@ -1,14 +1,7 @@
-# Starting comment
-^ This line will be used to demonstrate the use of fgrep
fenestrOS
fenestros
-# Another comment
555-5555
f
-
.fenestros
-
.fe
-
£
-# End comment

Now apply the patch file:

root@ubuntu1604:/tmp# patch < greptest.patch
patching file greptest

Finally, check the contents of the patched greptest file:

root@ubuntu1604:/tmp# cat greptest
fenestrOS
fenestros
555-5555
f
.fenestros
.fe
£

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the patch command to view the command line switches. [/stextbox]

The strings Command

The strings Command is used to extract any printable string in one or more object files or executables. An object fie is an intermediary file used when compiling.

The format of an object file is ELF (Executable and Linkable Format). This same format is also used for:

executables,

shared libraries,

core dumps.

Used as is, the command extracts all strings greater than 4 characters in length:

root@ubuntu1604:/tmp# strings /usr/bin/passwd | more
/lib64/ld-linux-x86-64.so.2
kgUa
libpam.so.0
_ITM_deregisterTMCloneTable
__gmon_start__
_Jv_RegisterClasses
_ITM_registerTMCloneTable
pam_start
pam_strerror
pam_chauthtok
pam_end
libpam_misc.so.0
misc_conv
libselinux.so.1
_init
is_selinux_enabled
security_getenforce
context_user_get
security_compute_av
matchpathcon
freecon
context_free
setfscreatecon
--More--

Print the offset within the file before each string:

root@ubuntu1604:/tmp# strings -t d /usr/bin/passwd | more
568 /lib64/ld-linux-x86-64.so.2
823 kgUa
4241 libpam.so.0
4253 _ITM_deregisterTMCloneTable
4281 __gmon_start__
4296 _Jv_RegisterClasses
4316 _ITM_registerTMCloneTable
4342 pam_start
4352 pam_strerror
4365 pam_chauthtok
4379 pam_end
4387 libpam_misc.so.0
4404 misc_conv
4414 libselinux.so.1
4430 _init
4436 is_selinux_enabled
4455 security_getenforce
4475 context_user_get
4492 security_compute_av
4512 matchpathcon
4525 freecon
4533 context_free
4546 setfscreatecon
--More--

The -t switch can take one of three arguments that specify the numbering system to use:

Argument	Numbering System
d	Decimal
o	Octal
x	Hexadecimal

The -n switch prints sequences of characters that are at least min-len characters long, instead of the default 4:

root@ubuntu1604:/tmp# strings -t d -n 15 /usr/bin/passwd | more
568 /lib64/ld-linux-x86-64.so.2
4253 _ITM_deregisterTMCloneTable
4296 _Jv_RegisterClasses
4316 _ITM_registerTMCloneTable
4387 libpam_misc.so.0
4414 libselinux.so.1
4436 is_selinux_enabled
4455 security_getenforce
4475 context_user_get
4492 security_compute_av
4774 __stack_chk_fail
4942 __errno_location
5405 __libc_start_main
5537 LIBPAM_MISC_1.0
34456 Usage: %s [options] [LOGIN]
34496 -a, --all report password status on all accounts
34568 -d, --delete delete the password for the named accoun
t
34648 -e, --expire force expire the password for the named
account
34736 -h, --help display this help message and exit
34808 -k, --keep-tokens change password only if expired
34880 -i, --inactive INACTIVE set password inactive after expiration
--More--

The -f switch prints the name of the file before each string:

root@ubuntu1604:/tmp# strings -f /bin/* | grep "(c)"
/bin/ntfscat: Copyright (c) 2003-2005 Richard Russon
/bin/ntfscat: Copyright (c) 2003-2005 Anton Altaparmakov
/bin/ntfscat: Copyright (c) 2003-2005 Szabolcs Szakacsits
/bin/ntfscat: Copyright (c) 2007 Yura Pakhuchiy
/bin/ntfscluster: Copyright (c) 2002-2003 Richard Russon
/bin/ntfscluster: Copyright (c) 2005 Anton Altaparmakov
/bin/ntfscluster: Copyright (c) 2005-2006 Szabolcs Szakacsits
/bin/ntfsfallocate: Copyright (c) 2013-2014 Jean-Pierre Andre
/bin/ntfsfix: Copyright (c) 2000-2006 Anton Altaparmakov
/bin/ntfsfix: Copyright (c) 2002-2006 Szabolcs Szakacsits
/bin/ntfsfix: Copyright (c) 2007 Yura Pakhuchiy
/bin/ntfsfix: Copyright (c) 2011-2015 Jean-Pierre Andre
/bin/ntfsinfo: Copyright (c)
/bin/ntfsls: Copyright (c) 2003-2005 Anton Altaparmakov
/bin/ntfsls: Copyright (c) 2003 Richard Russon
/bin/ntfsls: Copyright (c) 2004 Carmelo Kintana
/bin/ntfsls: Copyright (c) 2004 Giang Nguyen
/bin/ntfsls: Copyright (c) 2003 Lode Leroy
/bin/ntfsmove: Copyright (c) 2003 Richard Russon
/bin/ntfstruncate: Copyright (c) 2002-2005 Anton Altaparmakov
/bin/ntfstruncate: Copyright (c) 2003 Richard Russon
/bin/ntfswipe: Copyright (c) 2002-2005 Richard Russon
/bin/ntfswipe: Copyright (c) 2004 Yura Pakhuchiy
/bin/ping: @(#) Copyright (c) 1989 The Regents of the University of California.
/bin/ping6: @(#) Copyright (c) 1989 The Regents of the University of California..

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the strings command to view the command line switches. [/stextbox]

The comm Command

This command compares two text files and prints the differences to standard output:

root@ubuntu1604:/tmp# comm /etc/passwd /root/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
comm: file 1 is not in sorted order
comm: file 2 is not in sorted order
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
systemd-timesync:x:100:102:systemd Time Synchronization,,,:/run/systemd:/bin/false
systemd-network:x:101:103:systemd Network Management,,,:/run/systemd/netif:/bin/false
systemd-resolve:x:102:104:systemd Resolver,,,:/run/systemd/resolve:/bin/false
systemd-bus-proxy:x:103:105:systemd Bus Proxy,,,:/run/systemd:/bin/false
syslog:x:104:108::/home/syslog:/bin/false
_apt:x:105:65534::/nonexistent:/bin/false
messagebus:x:106:110::/var/run/dbus:/bin/false
uuidd:x:107:111::/run/uuidd:/bin/false
lightdm:x:108:114:Light Display Manager:/var/lib/lightdm:/bin/false
whoopsie:x:109:116::/nonexistent:/bin/false
avahi-autoipd:x:110:119:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false
avahi:x:111:120:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
dnsmasq:x:112:65534:dnsmasq,,,:/var/lib/misc:/bin/false
colord:x:113:123:colord colour management daemon,,,:/var/lib/colord:/bin/false
speech-dispatcher:x:114:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/false
hplip:x:115:7:HPLIP system user,,,:/var/run/hplip:/bin/false
kernoops:x:116:65534:Kernel Oops Tracking Daemon,,,:/:/bin/false
pulse:x:117:124:PulseAudio daemon,,,:/var/run/pulse:/bin/false
rtkit:x:118:126:RealtimeKit,,,:/proc:/bin/false
saned:x:119:127::/var/lib/saned:/bin/false
usbmux:x:120:46:usbmux daemon,,,:/var/lib/usbmux:/bin/false
trainee10:x:1000:1000:trainee,,,:/home/trainee:/bin/bash
sshd:x:121:65534::/var/run/sshd:/usr/sbin/nologin
Linux is great!
trainee:x:1000:1000:trainee,,,:/home/trainee:/bin/bash
sshd:x:121:65534::/var/run/sshd:/usr/sbin/nologin

[stextbox id='black' image='null'] Important: The lines to the left are those that only appear in the first file. The lines on the right are those that exist in both files. The lines in the middle are those that only exist in the second file. [/stextbox]

If you only want to see the lines common to both files, use the following command:

root@ubuntu1604:/tmp# comm -12 /etc/passwd /root/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
comm: file 1 is not in sorted order
comm: file 2 is not in sorted order
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
systemd-timesync:x:100:102:systemd Time Synchronization,,,:/run/systemd:/bin/false
systemd-network:x:101:103:systemd Network Management,,,:/run/systemd/netif:/bin/false
systemd-resolve:x:102:104:systemd Resolver,,,:/run/systemd/resolve:/bin/false
systemd-bus-proxy:x:103:105:systemd Bus Proxy,,,:/run/systemd:/bin/false
syslog:x:104:108::/home/syslog:/bin/false
_apt:x:105:65534::/nonexistent:/bin/false
messagebus:x:106:110::/var/run/dbus:/bin/false
uuidd:x:107:111::/run/uuidd:/bin/false
lightdm:x:108:114:Light Display Manager:/var/lib/lightdm:/bin/false
whoopsie:x:109:116::/nonexistent:/bin/false
avahi-autoipd:x:110:119:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false
avahi:x:111:120:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
dnsmasq:x:112:65534:dnsmasq,,,:/var/lib/misc:/bin/false
colord:x:113:123:colord colour management daemon,,,:/var/lib/colord:/bin/false
speech-dispatcher:x:114:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/false
hplip:x:115:7:HPLIP system user,,,:/var/run/hplip:/bin/false
kernoops:x:116:65534:Kernel Oops Tracking Daemon,,,:/:/bin/false
pulse:x:117:124:PulseAudio daemon,,,:/var/run/pulse:/bin/false
rtkit:x:118:126:RealtimeKit,,,:/proc:/bin/false
saned:x:119:127::/var/lib/saned:/bin/false
usbmux:x:120:46:usbmux daemon,,,:/var/lib/usbmux:/bin/false

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the comm command to view the command line switches. [/stextbox]

The head Command

The head command is used to display the first x lines of a file. The default value of x is 10:

root@ubuntu1604:/tmp# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin

You can change the default value of x by using the -n switch:

root@ubuntu1604:/tmp# head -n 15 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin

The command can also be used to display the first y bytes of a file by using the -c switch:

root@ubuntu1604:/tmp# head -c 150 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/noloroot@ubuntu1604:/tmp#

If the value of y is negative, head displays all bytes in the file except the last y bytes:

root@ubuntu1604:/tmp# head -c 150 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/noloroot@ubuntu1604:/tmp#
root@ubuntu1604:/tmp# head -c -150 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
systemd-timesync:x:100:102:systemd Time Synchronization,,,:/run/systemd:/bin/false
systemd-network:x:101:103:systemd Network Management,,,:/run/systemd/netif:/bin/false
systemd-resolve:x:102:104:systemd Resolver,,,:/run/systemd/resolve:/bin/false
systemd-bus-proxy:x:103:105:systemd Bus Proxy,,,:/run/systemd:/bin/false
syslog:x:104:108::/home/syslog:/bin/false
_apt:x:105:65534::/nonexistent:/bin/false
messagebus:x:106:110::/var/run/dbus:/bin/false
uuidd:x:107:111::/run/uuidd:/bin/false
lightdm:x:108:114:Light Display Manager:/var/lib/lightdm:/bin/false
whoopsie:x:109:116::/nonexistent:/bin/false
avahi-autoipd:x:110:119:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false
avahi:x:111:120:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
dnsmasq:x:112:65534:dnsmasq,,,:/var/lib/misc:/bin/false
colord:x:113:123:colord colour management daemon,,,:/var/lib/colord:/bin/false
speech-dispatcher:x:114:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/false
hplip:x:115:7:HPLIP system user,,,:/var/run/hplip:/bin/false
kernoops:x:116:65534:Kernel Oops Tracking Daemon,,,:/:/bin/false
pulse:x:117:124:PulseAudio daemon,,,:/var/run/pulse:/bin/false
rtkit:x:118:126:RealtimeKit,,,:/proc:/bin/false
saned:x:119:127::/var/lib/saned:/bin/false
usbmux:x:120:46root@ubuntu1604:/tmp#

Both x and y can accept multipliers:

root@ubuntu1604:/tmp# head -c 1b /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nroot@ubuntu1604:/tmp#
root@ubuntu1604:/tmp#
root@ubuntu1604:/tmp# head -c 512 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nroot@ubuntu1604:/tmp

The common multipliers are:

Multiplier	Number of bytes
b	512
KB	1000
K	1024
MB	1000*1000
M	1024*1024
GB	1000*1000*1000
G	1024*1024*1024

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the head command to view the command line switches. [/stextbox]

The tail Command

The tail command is used to display the last x lines of a file. The default value of x is 10:

root@ubuntu1604:/tmp# tail /etc/passwd
colord:x:113:123:colord colour management daemon,,,:/var/lib/colord:/bin/false
speech-dispatcher:x:114:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/false
hplip:x:115:7:HPLIP system user,,,:/var/run/hplip:/bin/false
kernoops:x:116:65534:Kernel Oops Tracking Daemon,,,:/:/bin/false
pulse:x:117:124:PulseAudio daemon,,,:/var/run/pulse:/bin/false
rtkit:x:118:126:RealtimeKit,,,:/proc:/bin/false
saned:x:119:127::/var/lib/saned:/bin/false
usbmux:x:120:46:usbmux daemon,,,:/var/lib/usbmux:/bin/false
trainee:x:1000:1000:trainee,,,:/home/trainee:/bin/bash
sshd:x:121:65534::/var/run/sshd:/usr/sbin/nolog

You can change the default value of x by using the -n switch:

root@ubuntu1604:/tmp# tail -n 15 /etc/passwd
lightdm:x:108:114:Light Display Manager:/var/lib/lightdm:/bin/false
whoopsie:x:109:116::/nonexistent:/bin/false
avahi-autoipd:x:110:119:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false
avahi:x:111:120:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
dnsmasq:x:112:65534:dnsmasq,,,:/var/lib/misc:/bin/false
colord:x:113:123:colord colour management daemon,,,:/var/lib/colord:/bin/false
speech-dispatcher:x:114:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/false
hplip:x:115:7:HPLIP system user,,,:/var/run/hplip:/bin/false
kernoops:x:116:65534:Kernel Oops Tracking Daemon,,,:/:/bin/false
pulse:x:117:124:PulseAudio daemon,,,:/var/run/pulse:/bin/false
rtkit:x:118:126:RealtimeKit,,,:/proc:/bin/false
saned:x:119:127::/var/lib/saned:/bin/false
usbmux:x:120:46:usbmux daemon,,,:/var/lib/usbmux:/bin/false
trainee:x:1000:1000:trainee,,,:/home/trainee:/bin/bash
sshd:x:121:65534::/var/run/sshd:/usr/sbin/nologin

The command can also be used to display the last y bytes of a file by using the -c switch:

root@ubuntu1604:/tmp# tail -c 150 /etc/passwd
:usbmux daemon,,,:/var/lib/usbmux:/bin/false
trainee:x:1000:1000:trainee,,,:/home/trainee:/bin/bash
sshd:x:121:65534::/var/run/sshd:/usr/sbin/nologin

If the value of y is positive, tail displays all bytes in the file after the yth byte:

root@ubuntu1604:/tmp# tail -c +150 /etc/passwd
ogin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
systemd-timesync:x:100:102:systemd Time Synchronization,,,:/run/systemd:/bin/false
systemd-network:x:101:103:systemd Network Management,,,:/run/systemd/netif:/bin/false
systemd-resolve:x:102:104:systemd Resolver,,,:/run/systemd/resolve:/bin/false
systemd-bus-proxy:x:103:105:systemd Bus Proxy,,,:/run/systemd:/bin/false
syslog:x:104:108::/home/syslog:/bin/false
_apt:x:105:65534::/nonexistent:/bin/false
messagebus:x:106:110::/var/run/dbus:/bin/false
uuidd:x:107:111::/run/uuidd:/bin/false
lightdm:x:108:114:Light Display Manager:/var/lib/lightdm:/bin/false
whoopsie:x:109:116::/nonexistent:/bin/false
avahi-autoipd:x:110:119:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false
avahi:x:111:120:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
dnsmasq:x:112:65534:dnsmasq,,,:/var/lib/misc:/bin/false
colord:x:113:123:colord colour management daemon,,,:/var/lib/colord:/bin/false
speech-dispatcher:x:114:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/false
hplip:x:115:7:HPLIP system user,,,:/var/run/hplip:/bin/false
kernoops:x:116:65534:Kernel Oops Tracking Daemon,,,:/:/bin/false
pulse:x:117:124:PulseAudio daemon,,,:/var/run/pulse:/bin/false
rtkit:x:118:126:RealtimeKit,,,:/proc:/bin/false
saned:x:119:127::/var/lib/saned:/bin/false
usbmux:x:120:46:usbmux daemon,,,:/var/lib/usbmux:/bin/false
trainee:x:1000:1000:trainee,,,:/home/trainee:/bin/bash
sshd:x:121:65534::/var/run/sshd:/usr/sbin/nologin

Both x and y can accept multipliers:

root@ubuntu1604:/tmp# tail -c 1b /etc/passwd
:114:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/false
hplip:x:115:7:HPLIP system user,,,:/var/run/hplip:/bin/false
kernoops:x:116:65534:Kernel Oops Tracking Daemon,,,:/:/bin/false
pulse:x:117:124:PulseAudio daemon,,,:/var/run/pulse:/bin/false
rtkit:x:118:126:RealtimeKit,,,:/proc:/bin/false
saned:x:119:127::/var/lib/saned:/bin/false
usbmux:x:120:46:usbmux daemon,,,:/var/lib/usbmux:/bin/false
trainee:x:1000:1000:trainee,,,:/home/trainee:/bin/bash
sshd:x:121:65534::/var/run/sshd:/usr/sbin/nologin
root@ubuntu1604:/tmp#
root@ubuntu1604:/tmp# tail -c 512 /etc/passwd
:114:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/false
hplip:x:115:7:HPLIP system user,,,:/var/run/hplip:/bin/false
kernoops:x:116:65534:Kernel Oops Tracking Daemon,,,:/:/bin/false
pulse:x:117:124:PulseAudio daemon,,,:/var/run/pulse:/bin/false
rtkit:x:118:126:RealtimeKit,,,:/proc:/bin/false
saned:x:119:127::/var/lib/saned:/bin/false
usbmux:x:120:46:usbmux daemon,,,:/var/lib/usbmux:/bin/false
trainee:x:1000:1000:trainee,,,:/home/trainee:/bin/bash
sshd:x:121:65534::/var/run/sshd:/usr/sbin/nologin

The common multipliers are:

Multiplier	Number of bytes
b	512
KB	1000
K	1024
MB	1000*1000
M	1024*1024
GB	1000*1000*1000
G	1024*1024*1024

A useful switch to use with the tail command is -f. This switch continually updates the output:

root@ubuntu1604:/tmp# tail -f /var/log/syslog
Dec 17 13:22:35 ubuntu1604 sh[870]: Sleeping '888' '888'
Dec 17 14:16:59 ubuntu1604 sh[870]: message repeated 465 times: [ Sleeping '888' '888']
Dec 17 14:17:01 ubuntu1604 CRON[17224]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Dec 17 14:17:06 ubuntu1604 sh[870]: Sleeping '888' '888'
Dec 17 15:17:00 ubuntu1604 sh[870]: message repeated 512 times: [ Sleeping '888' '888']
Dec 17 15:17:01 ubuntu1604 CRON[30516]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Dec 17 15:17:07 ubuntu1604 sh[870]: Sleeping '888' '888'
Dec 17 15:30:00 ubuntu1604 sh[870]: message repeated 110 times: [ Sleeping '888' '888']
Dec 17 15:30:00 ubuntu1604 systemd[1]: snapd.refresh.timer: Adding 4h 29min 43.172868s random time.
Dec 17 15:30:07 ubuntu1604 sh[870]: Sleeping '888' '888'
^C

Command Line Switches

[stextbox id='black' image='null'] To do : Use the –help option of the head command to view the command line switches. [/stextbox]

LAB #4 - Use the grep, tr and cut to extract your IP address from the output of ifconfig

root@ubuntu1604:/tmp# ifconfig enp0s3
enp0s3 Link encap:Ethernet HWaddr 08:00:27:20:4b:97
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::c4db:2b29:aedd:e7ed/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:230127 errors:0 dropped:0 overruns:0 frame:0
TX packets:149877 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:238004530 (238.0 MB) TX bytes:10864892 (10.8 MB)
root@ubuntu1604:/tmp# ifconfig enp0s3 | grep "inet"
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::c4db:2b29:aedd:e7ed/64 Scope:Link
root@ubuntu1604:/tmp# ifconfig enp0s3 | grep "inet" | grep -v "inet6"
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
root@ubuntu1604:/tmp# ifconfig enp0s3 | grep "inet" | grep -v "inet6" | tr -s " " ":"
:inet:addr:10.0.2.15:Bcast:10.0.2.255:Mask:255.255.255.0
root@ubuntu1604:/tmp# ifconfig enp0s3 | grep "inet" | grep -v "inet6" | tr -s " " ":" | cut -d: -f4
10.0.2.15

[stextbox id='black' image='null'] Important : Note the use of the -s switch with the tr command. This switch replaces a string of x identical characters with a single character. [/stextbox]

<html>

Copyright © 2011-2018 Hugh Norris.<br><br>

</html>