Ceci est une ancienne révision du document !


Table des matières

Version : 2022.01

Last updated : 2022/08/25 09:27

LCE404 - Basic Shell Commands and Text Manipulation Tools

Contents

  • LCE404 - Basic Shell Commands and Text Manipulation Tools
    • Contents
    • LAB #1 - Use of Basic Shell Commands
      • 1.1 - The stty Command
      • 1.2 - The date command
      • 1.3 - The who Command
      • 1.4 - The df Command
      • 1.5 - The free Command
      • 1.6 - The whoami Command
      • 1.7 - The pwd Command
      • 1.8 - The cd Command
      • 1.9 - The ls Command
      • 1.10 - The lsof Command
      • 1.11 - The touch Command
      • 1.12 - The echo Command
      • 1.13 - The cp Command
      • 1.14 - The file Command
      • 1.15 - The cat Command
      • 1.16 - The mv Command
      • 1.17 - The mkdir Command
      • 1.18 - The rmdir Command
      • 1.19 - The rm Command
      • 1.20 - The sort Command
      • 1.21 - The more Command
      • 1.22 - The less Command
      • 1.23 - The find Command
      • 1.24 - The su Command
      • 1.25 - The updatedb and locate Commands
      • 1.26 - The whereis Command
      • 1.27 - The which Command
      • 1.28 - The uptime Command
      • 1.29 - The w Command
      • 1.30 - The uname Command
      • 1.31 - The du Command
      • 1.32 - The clear Command
      • 1.33 - The exit Command
      • 1.34 - The logout Command
      • 1.35 - The sleep Command
      • 1.36 - The wall Command
      • 1.37 - The seq Command
      • 1.38 - The screen Command
    • LAB #2 - Switches and Arguments
    • LAB #3 - Regular Expressions
      • BREs
      • EREs
    • Manipulating Text Files
      • Text-search Utilities
        • The grep Command
        • The egrep Command
        • The fgrep Command
        • LAB #4 - Using grep, egrep and fgrep
      • The Stream EDitor SED
        • LAB #5 - Using sed
      • The Text Processor AWK
        • Presentation
        • Field Separation
        • Conditions
          • A regular expression applied to a record
          • A regular expression applied to a field
          • Comparisons
          • Logical Operators
          • Built-in Variables
        • Awk Scripts
        • The printf function
        • Control Statements
          • if
          • for
          • while
          • do-while
        • LAB #3 - Using awk
    • LAB 7 - Other Useful Commands
      • 7.1 - The expand Command
      • 7.2 - La Commande unexpand
      • 7.3 - The cut command
      • 7.4 - The uniq Command
      • 7.5 - The tr Command
      • 7.6 - The paste Command
      • 7.7 - The split Command
      • 7.8 - The diff Command
      • 7.9 - The cmp Command
      • 7.10 - The patch Command
      • 7.11 - The strings Command
      • 7.12 - The comm Command
      • 7.13 - The head Command
      • 7.14 - The tail Command
    • LAB #8 - Use the grep, tr and cut commands to extract your IPv4 address from the output of ifconfig
    • LAB #9 - Use the grep, awk and sed commands to extract your IPv4 address from the output of ip

LAB #1 - Use of Basic Shell Commands

To do - You are currently the root user in your terminal. Before proceeding further, type exit and hit the ↵ Enter key.

1.1 - 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@centos8 ~]$ stty -a
speed 38400 baud; rows 24; 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

Important - The two most important combinations are intr = ^C and susp = ^Z. The former kills the process whilst the latter suspends its execution.

Command Line Switches

To do : Use the –help option of the stty command to view the command line switches.

1.2 - 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@centos8 ~]$ date
Tue 20 Apr 02:27:55 EDT 2021

Command Line Switches

To do : Use the –help option of the date command to view the command line switches.

1.3 - The who Command

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

<code>
[trainee@centos8 ~]$ who
trainee  pts/0        2021-04-20 02:21 (10.0.2.2)

Command Line Switches

To do : Use the –help option of the who command to view the command line switches.

1.4 - The df Command

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

[trainee@centos8 ~]$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs         1897604       0   1897604   0% /dev
tmpfs            1913700       0   1913700   0% /dev/shm
tmpfs            1913700    8736   1904964   1% /run
tmpfs            1913700       0   1913700   0% /sys/fs/cgroup
/dev/sda3       15349760 2419808  12929952  16% /
/dev/sda1         289285  198275     71554  74% /boot
tmpfs             382740       0    382740   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@centos8 ~]$ df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        1.9G     0  1.9G   0% /dev
tmpfs           1.9G     0  1.9G   0% /dev/shm
tmpfs           1.9G  8.6M  1.9G   1% /run
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda3        15G  2.4G   13G  16% /
/dev/sda1       283M  194M   70M  74% /boot
tmpfs           374M     0  374M   0% /run/user/1000

Command Line Switches

To do : Use the –help option of the df command to view the command line switches.

1.5 - The free Command

This command's output shows the memory usage:

[trainee@centos8 ~]$ free

            total        used        free      shared  buff/cache   available

Mem: 3827400 290372 2594252 8736 942776 3281880 Swap: 2047996 0 2047996 </code>

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

<code>
[trainee@centos8 ~]$ free -h
              total        used        free      shared  buff/cache   available
Mem:          3.6Gi       283Mi       2.5Gi       8.0Mi       920Mi       3.1Gi
Swap:         2.0Gi          0B       2.0Gi

Command Line Switches

To do : Use the –help option of the free command to view the command line switches.

1.6 - The whoami Command

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

[trainee@centos8 ~]$ whoami
trainee

Now become the system administrator root:

[trainee@centos8 ~]$ su -
Password: fenestros

Important : Note that the password will not be visible.

Now use the whoami command again:

[root@centos8 ~]# whoami
root

Important : Note the current effective user ID is root.

Finally execute the exit command to return as trainee:

[root@centos8 ~]# exit
logout
[trainee@centos8 ~]$ 

Command Line Switches

To do : Use the –help option of the whoami command to view the command line switches.

1.7 - The pwd Command

This command's output shows the current working directory:

[trainee@centos8 ~]$ pwd
/home/trainee

Command Line Switches

To do : Use the help command with pwd option to view the command line switches.

1.8 - The cd Command

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

[trainee@centos8 ~]$ cd /tmp
[trainee@centos8 tmp]$ pwd
/tmp
[trainee@centos8 tmp]$ 

Command Line Switches

To do : Use the help command with cd option to view the command line switches.

1.9 - 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@centos8 tmp]$ ls
systemd-private-d9ff2376a8a44f0392f860d80c839be4-chronyd.service-6im4Ii

Command Line Switches

To do : Use the –help option of the ls command to view the command line switches.

1.10 - The lsof Command

This command's output shows information about open files:

[trainee@centos8 tmp]$ su -
Password: fenestros
[root@centos8 ~]# lsof | more
COMMAND     PID   TID TASKCMD             USER   FD      TYPE             DEVICE
 SIZE/OFF       NODE NAME
systemd       1                           root  cwd       DIR                8,3
      224        128 /
systemd       1                           root  rtd       DIR                8,3
      224        128 /
systemd       1                           root  txt       REG                8,3
  1609248    8811152 /usr/lib/systemd/systemd
systemd       1                           root  mem       REG                8,3
  2191808     126296 /usr/lib64/libm-2.28.so
systemd       1                           root  mem       REG                8,3
   628744     126019 /usr/lib64/libudev.so.1.6.11
systemd       1                           root  mem       REG                8,3
   969832     151279 /usr/lib64/libsepol.so.1
systemd       1                           root  mem       REG                8,3
  1805368     179753 /usr/lib64/libunistring.so.2.1.0
systemd       1                           root  mem       REG                8,3
   303944     131440 /usr/lib64/libpcap.so.1.9.1
systemd       1                           root  mem       REG                8,3
   145984     163438 /usr/lib64/libgpg-error.so.0.24.2
systemd       1                           root  mem       REG                8,3
    71528     194381 /usr/lib64/libjson-c.so.4.0.0
systemd       1                           root  mem       REG                8,3
--More--

Command Line Switches

To do : Use the –help option of the lsof command to view the command line switches.

1.11 - 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@centos8 ~]# exit
logout
[trainee@centos8 tmp]$ touch test
[trainee@centos8 tmp]$ ls
systemd-private-d9ff2376a8a44f0392f860d80c839be4-chronyd.service-6im4Ii  test

Command Line Switches

To do : Use the –help option of the touch command to view the command line switches.

1.12 - The echo Command

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

[trainee@centos8 tmp]$ echo fenestros
fenestros

Command Line Switches

To do : Use the help command with echo option to view the command line switches.

1.13 - The cp Command

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

[trainee@centos8 tmp]$ cp test ~
[trainee@centos8 tmp]$ ls -l ~
total 0
-rw-rw-r--. 1 trainee trainee 0 Apr 20 03:36 test

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.

Command Line Switches

To do : Use the –help option of the cp command to view the command line switches.

1.14 - The file Command

This command determines a file type:

[trainee@centos8 tmp]$ file ~/test
/home/trainee/test: empty

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.

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

[trainee@centos8 tmp]$ echo "fenestros" > ~/test

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

[trainee@centos8 tmp]$ file ~/test
/home/trainee/test: ASCII text

Command Line Switches

To do : Use the –help option of the file command to view the command line switches.

1.15 - 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@centos8 tmp]$ cat ~/test
fenestros

Command Line Switches

To do : Use the –help option of the cat command to view the command line switches.

1.16 - The mv Command

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

[trainee@centos8 tmp]$ mv ~/test .
[trainee@centos8 tmp]$ ls -l ~
total 0
[trainee@centos8 tmp]$ mv test TeSt
[trainee@centos8 tmp]$ ls -l
total 4
drwx------. 3 root    root    17 Apr 19 12:05 systemd-private-d9ff2376a8a44f0392f860d80c839be4-chronyd.service-6im4Ii
-rw-rw-r--. 1 trainee trainee 10 Apr 20 03:38 TeSt

Important - Note the use of the shortcut . which indicates the current working directory.

Command Line Switches

To do : Use the –help option of the mv command to view the command line switches.

1.17 - The mkdir Command

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

[trainee@centos8 tmp]$ cd ~
[trainee@centos8 ~]$ mkdir testdir
[trainee@centos8 ~]$ ls
testdir

Command Line Switches

To do : Use the –help option of the mkdir command to view the command line switches.

1.18 - The rmdir Command

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

[trainee@centos8 ~]$ rmdir testdir
[trainee@centos8 ~]$ ls
[trainee@centos8 ~]$ 

Command Line Switches

To do : Use the –help option of the rmdir command to view the command line switches.

1.19 - The rm Command

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

[trainee@centos8 ~]$ mkdir testdir1
[trainee@centos8 ~]$ cd /tmp
[trainee@centos8 tmp]$ echo "fenestros" > TeSt
[trainee@centos8 tmp]$ cd ~
[trainee@centos8 ~]$ mv /tmp/TeSt ~/testdir1
[trainee@centos8 ~]$ ls -lR testdir1/
testdir1/:
total 4
-rw-rw-r--. 1 trainee trainee 10 Apr 20 03:44 TeSt
[trainee@centos8 ~]$ rmdir testdir1/
rmdir: failed to remove 'testdir1/': Directory not empty
[trainee@centos8 ~]$ rm -rf testdir1/
[trainee@centos8 ~]$ ls
[trainee@centos8 ~]$ 

Command Line Switches

To do : Use the –help option of the rm command to view the command line switches.

1.20 - The sort Command

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

[trainee@centos8 ~]$ touch aac abc bca xyz
[trainee@centos8 ~]$ ls
aac  abc  bca  xyz
[trainee@centos8 ~]$ ls | sort
aac
abc
bca
xyz
[trainee@centos8 ~]$ ls | sort -r
xyz
bca
abc
aac

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.

Command Line Switches

To do : Use the –help option of the sort command to view the command line switches.

1.21 - The more Command

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

[trainee@centos8 ~]$ more /etc/services
# /etc/services:
# $Id: services,v 1.49 2017/08/18 12:43:23 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2016-07-08
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, most entries here have two entries
# even if the protocol doesn't support UDP operations.
# Updated from RFC 1700, ``Assigned Numbers'' (October 1994).  Not all ports
# are included, only the more common ones.
#
# The latest IANA port assignments can be gotten from
#       http://www.iana.org/assignments/port-numbers
# The Well Known Ports are those from 0 through 1023.
# The Registered Ports are those from 1024 through 49151
# The Dynamic and/or Private Ports are those from 49152 through 65535
#
# Each line describes one service, and is of the form:
#
# service-name  port/protocol  [aliases ...]   [# comment]

tcpmux          1/tcp                           # TCP port service multiplexer
--More--(0%)

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.

Command Line Switches

To do : Use the –help option of the more command to view the command line switches.

1.22 - 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@centos8 ~]$ less /etc/services
# /etc/services:
# $Id: services,v 1.49 2017/08/18 12:43:23 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2016-07-08
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, most entries here have two entries
# even if the protocol doesn't support UDP operations.
# Updated from RFC 1700, ``Assigned Numbers'' (October 1994).  Not all ports
# are included, only the more common ones.
#
# The latest IANA port assignments can be gotten from
#       http://www.iana.org/assignments/port-numbers
# The Well Known Ports are those from 0 through 1023.
# The Registered Ports are those from 1024 through 49151
# The Dynamic and/or Private Ports are those from 49152 through 65535
#
# Each line describes one service, and is of the form:
#
# service-name  port/protocol  [aliases ...]   [# comment]

tcpmux          1/tcp                           # TCP port service multiplexer
/etc/services

Command Line Switches

To do : Use the –help option of the less command to view the command line switches.

1.23 - 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@centos8 ~]$ find acc
find: ‘acc’: No such file or directory
[trainee@centos8 ~]$ find aac
aac

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.

Command Line Switches

To do : Use the –help option of the find command to view the command line switches.

1.24 - 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@centos8 ~]$ su -
Password: fenestros
[root@centos8 ~]# 

Important : Note that the password will not be visible.

Command Line Switches

To do : Use the –help option of the su command to view the command line switches.

1.25 - 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@centos8 ~]# ls -l /var/lib/mlocate/mlocate.db
-rw-r-----. 1 root slocate 1652822 Apr 20 03:52 /var/lib/mlocate/mlocate.db

Important : For information concerning the database format, please see man 5 locatedb.

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

[root@centos8 ~]# cat /etc/updatedb.conf
PRUNE_BIND_MOUNTS = "yes"
PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fuse.sshfs fusectl gfs gfs2 gpfs hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs ceph fuse.ceph"
PRUNENAMES = ".git .hg .svn .bzr .arch-ids {arch} CVS"
PRUNEPATHS = "/afs /media /mnt /net /sfs /tmp /udev /var/cache/ccache /var/lib/yum/yumdb /var/lib/dnf/yumdb /var/spool/cups /var/spool/squid /var/tmp /var/lib/ceph"

Use of these two commands is very simple:

[root@centos8 ~]# updatedb
[root@centos8 ~]# locate aac
/home/trainee/aac
/usr/lib/.build-id/10/3879896070484de2e0eaac453ec43579fbb8e2
/usr/lib/.build-id/22/e20bcaaca5ed745d4f5f319ce3cc15e0db8d56
/usr/lib/.build-id/32/ec211a3c0121b5439deba3d4d1fb4e9bf97aac
/usr/lib/.build-id/45/bdf33b3fef84664d179d00e997daac1648973b
/usr/lib/.build-id/4c/5a4eaaca9035bbc2fbf200bd71eda505b568f2
/usr/lib/.build-id/55/2e764cb2c3aaacaeec1027b10ce22fb78a5896
/usr/lib/.build-id/55/769aac2caf22de496d9a08e5438d600ff92d6f
/usr/lib/.build-id/55/e2c36fd33cb232e58642c962aac3eeac1168be
/usr/lib/.build-id/65/33988a76dd4aac6840cc9d04ae13ac39869b13
/usr/lib/.build-id/79/dc2cf8dc9bd9617d5e897faac79f7458856f19
/usr/lib/.build-id/7e/ec6d530511aac280c13786861e4ec2c04321d9
/usr/lib/.build-id/8a/f8d384f0b01b7bd13a3aac719f738051e20351
/usr/lib/.build-id/8f/0de63d065be5aac7c552cce9412c25e0fa1e3a
/usr/lib/.build-id/93/8ee8bf115b7e98e9b83a68d8a679cd4a7b0aac
/usr/lib/.build-id/9a/635a7bcba89b0280c2198ea30d02faaacaac48
/usr/lib/.build-id/ac/8c79a966aacb69715f0fb388949dfc48ae88c8
/usr/lib/.build-id/b0/14a927cc892e6c5a8faaca7396a5a1c544f599
/usr/lib/.build-id/b2/cee7e58576f3167aacb613906c9790c5bcd266
/usr/lib/.build-id/ea/6926b2fcdbc231b6aa7511c568de704aac79b6
/usr/lib/.build-id/f1/b2efd3d36aecaac279b4a77d00657356b2585f
/usr/lib/.build-id/fc/7d382132889a539aac9cd499f1a2b56855bd95
/usr/lib/modules/4.18.0-147.8.1.el8_1.x86_64/kernel/drivers/scsi/aacraid
/usr/lib/modules/4.18.0-147.8.1.el8_1.x86_64/kernel/drivers/scsi/aacraid/aacraid.ko.xz
/usr/lib/modules/4.18.0-240.22.1.el8_3.x86_64/kernel/drivers/scsi/aacraid
/usr/lib/modules/4.18.0-240.22.1.el8_3.x86_64/kernel/drivers/scsi/aacraid/aacraid.ko.xz
/usr/share/mime/audio/aac.xml

Command Line Switches

To do : Use the –help option of the updatedb and locate commands to view their command line switches.

1.26 - 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@centos8 ~]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz

Command Line Switches

To do : Use the –help option of the whereis command to view the command line switches.

1.27 - The which Command

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

[root@centos8 ~]# which passwd
/usr/bin/passwd

Command Line Switches

To do : Use the –help option of the which command to view the command line switches.

1.28 - 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@centos8 ~]# uptime
 03:55:13 up 15:50,  1 user,  load average: 0.01, 0.00, 0.00

Command Line Switches

The switches associated with this command are:

To do : Use the –help option of the uptime command to view the command line switches.

1.29 - 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@centos8 ~]# w
 03:55:16 up 15:50,  1 user,  load average: 0.01, 0.00, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
trainee  pts/0    10.0.2.2         02:21    4.00s  0.16s  0.01s 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

To do : Use the –help option of the w command to view the command line switches.

1.30 - The uname Command

This command prints system information to the standard output:

[root@centos8 ~]# uname -a
Linux centos8.ittraining.loc 4.18.0-240.22.1.el8_3.x86_64 #1 SMP Thu Apr 8 19:01:30 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
[root@centos8 ~]# uname -s
Linux
[root@centos8 ~]# uname -n
centos8.ittraining.loc
[root@centos8 ~]# uname -r
4.18.0-240.22.1.el8_3.x86_64
[root@centos8 ~]# uname -v
#1 SMP Thu Apr 8 19:01:30 UTC 2021
[root@centos8 ~]# uname -m
x86_64
[root@centos8 ~]# uname -p
x86_64
[root@centos8 ~]# uname -i
x86_64
[root@centos8 ~]# uname -o
GNU/Linux

Command Line Switches

To do : Use the –help option of the uname command to view the command line switches.

1.31 - The du Command

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

[root@centos8 ~]# du -sh /* 2>/dev/null
0	/bin
192M	/boot
0	/dev
23M	/etc
16K	/home
0	/lib
0	/lib64
0	/media
0	/mnt
0	/opt
0	/proc
32K	/root
8.6M	/run
0	/sbin
0	/srv
0	/sys
0	/tmp
1.9G	/usr
211M	/var

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.

Command Line Switches

To do : Use the –help option of the du command to view the command line switches.

1.32 - The clear Command

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

[root@centos8 ~]# clear

[root@centos8 ~]# 

1.33 - The exit Command

This command exits the current shell:

[root@centos8 ~]# exit
logout
[trainee@centos8 ~]$  

Command Line Switches

To do : Use the help command with exit option to view the command line switches.

1.34 - 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

To do : Use the help command with logout option to view the command line switches.

1.35 - The sleep Command

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

[trainee@centos8 ~]$ sleep 10

Command Line Switches

To do : Use the –help option of the sleep command to view the command line switches.

1.36 - The wall Command

Wall sends a message to everybody logged in with their mesg(1) permission set to yes. The message can be given as an argument to wall, or it can be sent to wall's standard input. When using the standard input from a terminal, the message should be terminated with the EOF key (usually Control-D). The length of the message is limited to 20 lines. For every invocation of wall a notification will be written to syslog, with facility LOG_USER and level LOG_INFO.

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

[trainee@centos8 ~]$ su -
Password: 
[root@centos8 ~]# wall this is a message from root
                                                                               
Broadcast message from trainee@centos8.ittraining.loc (pts/0) (Tue Apr 20 04:11
                                                                               
this is a message from root
                                                                               
[root@centos8 ~]#

In the second session you should see the following message :

Activate the web console with: systemctl enable --now cockpit.socket

Last login: Tue Apr 20 02:21:15 2021 from 10.0.2.2
                                                                               
Broadcast message from trainee@centos8.ittraining.loc (pts/0) (Tue Apr 20 04:11
                                                                               
this is a message from root
[Enter]                                                                       
[trainee@centos8 ~]$ 
                                                                           

La commande wall ignore la variable d’environnement TZ. L’heure affichée dans la première page est basée sur les paramètres de régionalisation du système :

[root@centos8 ~]# date
Tue Apr 20 04:23:19 EDT 2021

1.37 - 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@centos8 ~]# seq 10
1
2
3
4
5
6
7
8
9
10
[root@centos8 ~]# seq 20 30
20
21
22
23
24
25
26
27
28
29
30
[root@centos8 ~]# seq 20 10 90
20
30
40
50
60
70
80
90
[root@centos8 ~]# 

Command Line Switches

To do : Use the –help option of the seq command to view the command line switches.

1.38 - 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 RHEL/CentOS 7:

[root@centos8 ~]#  which screen
/usr/bin/which: no screen in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)

[root@centos8 ~]# dnf makecache
CentOS Linux 8 - AppStream                                                                                                                                                                                    7.9 kB/s | 4.3 kB     00:00    
CentOS Linux 8 - BaseOS                                                                                                                                                                                        31 kB/s | 3.9 kB     00:00    
CentOS Linux 8 - Extras                                                                                                                                                                                        17 kB/s | 1.5 kB     00:00    
Metadata cache created.

[root@centos8 ~]# dnf install screen -y
Last metadata expiration check: 0:00:04 ago on Tue 20 Apr 2021 04:33:50 AM EDT.
No match for argument: screen
Error: Unable to find a match: screen

[root@centos8 ~]# dnf install epel-release -y
...                                                                                                                                                                             1/1 
Installed:
  epel-release-8-8.el8.noarch                                                                                                                                                                                                                 

Complete!

[root@centos8 ~]# dnf install screen -y
Extra Packages for Enterprise Linux Modular 8 - x86_64                                                                                                                                                        522 kB/s | 559 kB     00:01    
Extra Packages for Enterprise Linux 8 - x86_64                                                                                                                                                                2.5 MB/s | 9.4 MB     00:03    
Last metadata expiration check: 0:00:01 ago on Tue 20 Apr 2021 04:36:11 AM EDT.
Dependencies resolved.
==============================================================================================================================================================================================================================================
 Package                                                 Architecture                                            Version                                                          Repository                                             Size
==============================================================================================================================================================================================================================================
Installing:
 screen                                                  x86_64                                                  4.6.2-10.el8                                                     epel                                                  582 k

Transaction Summary
==============================================================================================================================================================================================================================================
Install  1 Package

Total download size: 582 k
Installed size: 971 k
Downloading Packages:
screen-4.6.2-10.el8.x86_64.rpm                                                                                                                                                                                283 kB/s | 582 kB     00:02    
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                         229 kB/s | 582 kB     00:02     
warning: /var/cache/dnf/epel-6519ee669354a484/packages/screen-4.6.2-10.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 2f86d6a1: NOKEY
Extra Packages for Enterprise Linux 8 - x86_64                                                                                                                                                                1.6 MB/s | 1.6 kB     00:00    
Importing GPG key 0x2F86D6A1:
 Userid     : "Fedora EPEL (8) <epel@fedoraproject.org>"
 Fingerprint: 94E2 79EB 8D8F 25B2 1810 ADF1 21EA 45AB 2F86 D6A1
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                                                      1/1 
  Running scriptlet: screen-4.6.2-10.el8.x86_64                                                                                                                                                                                           1/1 
  Installing       : screen-4.6.2-10.el8.x86_64                                                                                                                                                                                           1/1 
  Running scriptlet: screen-4.6.2-10.el8.x86_64                                                                                                                                                                                           1/1 
  Verifying        : screen-4.6.2-10.el8.x86_64                                                                                                                                                                                           1/1 

Installed:
  screen-4.6.2-10.el8.x86_64                                                                                                                                                                                                                  

Complete!

[root@centos8 ~]# which screen
/usr/bin/screen

Create a session with screen:

[root@centos8 ~]# screen -S mysession

Important - When using RHEL/CentOS 8, you will notice that the title of the window holding the ssh session changes to [screen 0: root@centos8:~].

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

Important - When using RHEL/CentOS 8, you will notice that the title of the window holding the ssh session changes to [screen 1: root@centos8:~].

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@centos8 ~]# screen -ls
There is a screen on:
        12149.mysession (Attached)
1 Socket in /run/screen/S-root.

Now enter the following commands:

[root@centos8 ~]# sleep 9999 &
[1] 12187
[root@centos8 ~]# jobs -l
[1]+ 12187 Running                 sleep 9999 &
[root@centos8 ~]# 

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

<code>
[root@centos8 ~]# screen -S mysession
[detached from 12149.mysession]
[root@centos8 ~]# 

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

[root@centos8 ~]# screen -r

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

[root@centos8 ~]# jobs -l
[1]+ 12187 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@centos8 ~]# screen -S mysession
[detached from 12149.mysession]
[root@centos8 ~]# 

Now create a new, non-nested screen:

[root@centos8 ~]# screen -S mysession1

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

[root@centos8 ~]# screen -ls
There are screens on:
        12191.mysession1        (Attached)
        12149.mysession (Detached)
2 Sockets in /run/screen/S-root.

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

[root@centos8 ~]# screen -r 12149

[root@centos8 ~]# screen -ls
There is a screen on:
        12149.mysession (Attached)
1 Socket in /run/screen/S-root.
[root@centos8 ~]# sleep 9999 &
[1] 12187
[root@centos8 ~]# jobs -l
[1]+ 12187 Running                 sleep 9999 &
[root@centos8 ~]# jobs -l
[1]+ 12187 Running                 sleep 9999 &
[root@centos8 ~]# 
[root@centos8 ~]# screen -ls
There are screens on:
        12191.mysession1        (Attached)
        12149.mysession (Attached)
2 Sockets in /run/screen/S-root.

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

[root@centos8 ~]# which screen
/usr/bin/screen
[root@centos8 ~]# screen -S mysession
[detached from 12149.mysession]
[root@centos8 ~]# screen -r
[detached from 12149.mysession]
[root@centos8 ~]# screen -S mysession1
[detached from 12191.mysession1]
[root@centos8 ~]# 

Now check which screen you are connected to:

[root@centos8 ~]# screen -ls
There are screens on:
	12191.mysession1	(Detached)
	12149.mysession	(Attached)
2 Sockets in /run/screen/S-root.

Finally, kill the two sessions:

[root@centos8 ~]# screen -XS 12191 quit
[root@centos8 ~]# screen -XS 12149 quit
[root@centos8 ~]# screen -ls
No Sockets found in /run/screen/S-root.

Command Line Switches

To do : Use the –help option of the screen command to view the command line switches.

LAB #2 - 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@centos8 ~]# ls -lai /tmp
total 0
16800396 drwxrwxrwt.  8 root root 172 Apr 20 04:36 .
     128 dr-xr-xr-x. 17 root root 224 Apr 19 11:50 ..
25447488 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .font-unix
 8388741 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .ICE-unix
     143 drwx------.  3 root root  17 Apr 19 12:05 systemd-private-d9ff2376a8a44f0392f860d80c839be4-chronyd.service-6im4Ii
  621976 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .Test-unix
  621954 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .X11-unix
17319048 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .XIM-unix
[root@centos8 ~]# ls -ali /tmp
total 0
16800396 drwxrwxrwt.  8 root root 172 Apr 20 04:36 .
     128 dr-xr-xr-x. 17 root root 224 Apr 19 11:50 ..
25447488 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .font-unix
 8388741 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .ICE-unix
     143 drwx------.  3 root root  17 Apr 19 12:05 systemd-private-d9ff2376a8a44f0392f860d80c839be4-chronyd.service-6im4Ii
  621976 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .Test-unix
  621954 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .X11-unix
17319048 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .XIM-unix
[root@centos8 ~]# ls -ial /tmp
total 0
16800396 drwxrwxrwt.  8 root root 172 Apr 20 04:36 .
     128 dr-xr-xr-x. 17 root root 224 Apr 19 11:50 ..
25447488 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .font-unix
 8388741 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .ICE-unix
     143 drwx------.  3 root root  17 Apr 19 12:05 systemd-private-d9ff2376a8a44f0392f860d80c839be4-chronyd.service-6im4Ii
  621976 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .Test-unix
  621954 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .X11-unix
17319048 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .XIM-unix

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

[root@centos8 ~]# ls -l --all --inode /tmp
total 0
16800396 drwxrwxrwt.  8 root root 172 Apr 20 04:36 .
     128 dr-xr-xr-x. 17 root root 224 Apr 19 11:50 ..
25447488 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .font-unix
 8388741 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .ICE-unix
     143 drwx------.  3 root root  17 Apr 19 12:05 systemd-private-d9ff2376a8a44f0392f860d80c839be4-chronyd.service-6im4Ii
  621976 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .Test-unix
  621954 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .X11-unix
17319048 drwxrwxrwt.  2 root root   6 Apr 19 11:37 .XIM-unix
[root@centos8 ~]# ls -l --allinode /tmp
ls: unrecognized option '--allinode'
Try 'ls --help' for more information.

Important - You should not combine any short options that take an argument.

LAB #3 - Regular Expressions

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 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

Manipulating Text Files

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

To do : Use the –help option of the grep command to view the command line switches.

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

To do : Use the –help option of the egrep command to view the command line switches.

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.

To do : Use the –help option of the fgrep command to view the command line switches.

LAB #4 - Using grep, egrep and fgrep

Create the following file:

[root@centos8 ~]# cd /tmp
[root@centos8 tmp]# vi greptest
[root@centos8 tmp]# cat greptest
fenestrOS
fenestros
555-5555
f
.fenestros
.fe
£

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

[root@centos8 ~]# 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@centos8 ~]# 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@centos8 ~]# 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@centos8 ~]# grep '^.$' /tmp/greptest
f
£

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

[root@centos8 ~]# grep '^.$' /tmp/greptest
f
£

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”.

Make the following changes to the greptest file:

[root@centos8 tmp]# vi greptest
[root@centos8 tmp]# cat greptest
# Starting comment
fenestrOS
fenestros
# Another comment
555-5555
f

.fenestros

.fe

£
# End comment

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

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

Important - The expression '^(#|$)' matches all lines beginning with the # character OR all lines with zero characters between the start and the end of the line.

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

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

Important: The above command is very useful when you want to quickly ascertain which directives are active in a very long configuration file.

Make the following changes to the greptest file:

[root@centos8 tmp]# vi greptest
[root@centos8 tmp]# cat 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

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

[root@centos8 ~]# 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@centos8 ~]# 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@centos8 ~]# 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

To do : Use the –help option of the fgrep command to view the command line switches.

LAB #5 - Using sed

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

[root@centos8 ~]# sed '1,10d' /etc/services | more
# are included, only the more common ones.
#
# The latest IANA port assignments can be gotten from
#       http://www.iana.org/assignments/port-numbers
# The Well Known Ports are those from 0 through 1023.
# The Registered Ports are those from 1024 through 49151
# The Dynamic and/or Private Ports are those from 49152 through 65535
#
# Each line describes one service, and is of the form:
#
# service-name  port/protocol  [aliases ...]   [# comment]

tcpmux          1/tcp                           # TCP port service multiplexer
tcpmux          1/udp                           # TCP port service multiplexer
rje             5/tcp                           # Remote Job Entry
rje             5/udp                           # Remote Job Entry
echo            7/tcp
echo            7/udp
discard         9/tcp           sink null
discard         9/udp           sink null
systat          11/tcp          users
systat          11/udp          users
daytime         13/tcp
--Plus--                    

Now display the same file without any commented lines:

[root@centos8 ~]# sed '/^#/d' /etc/services | more

tcpmux          1/tcp                           # TCP port service multiplexer
tcpmux          1/udp                           # TCP port service multiplexer
rje             5/tcp                           # Remote Job Entry
rje             5/udp                           # Remote Job Entry
echo            7/tcp
echo            7/udp
discard         9/tcp           sink null
discard         9/udp           sink null
systat          11/tcp          users
systat          11/udp          users
daytime         13/tcp
daytime         13/udp
qotd            17/tcp          quote
qotd            17/udp          quote
msp             18/tcp                          # message send protocol
msp             18/udp                          # message send protocol
chargen         19/tcp          ttytst source
chargen         19/udp          ttytst source
ftp-data        20/tcp
ftp-data        20/udp
ftp             21/tcp
ftp             21/udp          fsp fspd
--Plus--                        

Important: Note that the BRE is preceeded and followed by the / character.

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

[root@centos8 ~]# sed '1,2p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
...

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.

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

<code>
[root@centos8 ~]# sed -n '1,2p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/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@centos8 ~]# sed -n '/^#/!w /tmp/sedtest' /etc/services
[root@centos8 ~]# more /tmp/sedtest

tcpmux          1/tcp                           # TCP port service multiplexer
tcpmux          1/udp                           # TCP port service multiplexer
rje             5/tcp                           # Remote Job Entry
rje             5/udp                           # Remote Job Entry
echo            7/tcp
echo            7/udp
discard         9/tcp           sink null
discard         9/udp           sink null
systat          11/tcp          users
systat          11/udp          users
daytime         13/tcp
daytime         13/udp
qotd            17/tcp          quote
qotd            17/udp          quote
msp             18/tcp                          # message send protocol
msp             18/udp                          # message send protocol
chargen         19/tcp          ttytst source
chargen         19/udp          ttytst source
ftp-data        20/tcp
ftp-data        20/udp
ftp             21/tcp
ftp             21/udp          fsp fspd
--Plus--(0%)

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.

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

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

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.

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@centos8 tmp]# ls -l | awk '{print $8 $3 $4}'

05:23rootroot
05:21rootroot
05:28rootroot
05:29rootroot
12:05rootroot

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@centos8 tmp]# ls -l | awk '{print $8 " " $3 " " $4}'
  
05:23 root root
05:21 root root
05:28 root root
05:29 root root
12:05 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@centos8 tmp]# cat > scriptawk
BEGIN {
  print "Liste des systèmes de fichiers montés"}
{print $0}
END {
  print "====================================="}
[^D]

Now apply the awk script to /etc/fstab :

[root@centos8 tmp]# awk -f scriptawk /etc/mtab
Liste des systèmes de fichiers montés
sysfs /sys sysfs rw,seclabel,nosuid,nodev,noexec,relatime 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
devtmpfs /dev devtmpfs rw,seclabel,nosuid,size=1897604k,nr_inodes=474401,mode=755 0 0
securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0
tmpfs /dev/shm tmpfs rw,seclabel,nosuid,nodev 0 0
devpts /dev/pts devpts rw,seclabel,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
tmpfs /run tmpfs rw,seclabel,nosuid,nodev,mode=755 0 0
tmpfs /sys/fs/cgroup tmpfs ro,seclabel,nosuid,nodev,noexec,mode=755 0 0
cgroup /sys/fs/cgroup/systemd cgroup rw,seclabel,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd 0 0
pstore /sys/fs/pstore pstore rw,seclabel,nosuid,nodev,noexec,relatime 0 0
bpf /sys/fs/bpf bpf rw,nosuid,nodev,noexec,relatime,mode=700 0 0
cgroup /sys/fs/cgroup/hugetlb cgroup rw,seclabel,nosuid,nodev,noexec,relatime,hugetlb 0 0
cgroup /sys/fs/cgroup/devices cgroup rw,seclabel,nosuid,nodev,noexec,relatime,devices 0 0
cgroup /sys/fs/cgroup/cpuset cgroup rw,seclabel,nosuid,nodev,noexec,relatime,cpuset 0 0
cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,seclabel,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0
cgroup /sys/fs/cgroup/net_cls,net_prio cgroup rw,seclabel,nosuid,nodev,noexec,relatime,net_cls,net_prio 0 0
cgroup /sys/fs/cgroup/rdma cgroup rw,seclabel,nosuid,nodev,noexec,relatime,rdma 0 0
cgroup /sys/fs/cgroup/freezer cgroup rw,seclabel,nosuid,nodev,noexec,relatime,freezer 0 0
cgroup /sys/fs/cgroup/perf_event cgroup rw,seclabel,nosuid,nodev,noexec,relatime,perf_event 0 0
cgroup /sys/fs/cgroup/pids cgroup rw,seclabel,nosuid,nodev,noexec,relatime,pids 0 0
cgroup /sys/fs/cgroup/blkio cgroup rw,seclabel,nosuid,nodev,noexec,relatime,blkio 0 0
cgroup /sys/fs/cgroup/memory cgroup rw,seclabel,nosuid,nodev,noexec,relatime,memory 0 0
none /sys/kernel/tracing tracefs rw,seclabel,relatime 0 0
configfs /sys/kernel/config configfs rw,relatime 0 0
/dev/sda3 / xfs rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota 0 0
selinuxfs /sys/fs/selinux selinuxfs rw,relatime 0 0
systemd-1 /proc/sys/fs/binfmt_misc autofs rw,relatime,fd=36,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=1976 0 0
debugfs /sys/kernel/debug debugfs rw,seclabel,relatime 0 0
hugetlbfs /dev/hugepages hugetlbfs rw,seclabel,relatime,pagesize=2M 0 0
mqueue /dev/mqueue mqueue rw,seclabel,relatime 0 0
/dev/sda1 /boot ext4 rw,seclabel,relatime 0 0
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw,relatime 0 0
tmpfs /run/user/1000 tmpfs rw,seclabel,nosuid,nodev,relatime,size=382740k,mode=700,uid=1000,gid=1000 0 0
binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc rw,relatime 0 0
tracefs /sys/kernel/debug/tracing tracefs rw,seclabel,relatime 0 0
=====================================

Important: Note the use of the -f switch which instructs awk to use the script.

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

To do : Use the –help option of the awk command to view the command line switches.

LAB #6 - Using awk

Create the file sales.txt:

[root@centos8 tmp]# vi sales.txt
[root@centos8 tmp]# cat sales.txt
# 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 create the awk script sales.awk:

[root@centos8 tmp]# vi sales.awk
[root@centos8 tmp]# cat sales.awk 
# 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@centos8 tmp]# awk -f /tmp/sales.awk /tmp/sales.txt
PC Type :  Portables 	 Sales (06+13+83) :        175
PC Type :  Ipads 	 Sales (06+13+83) :         76
PC Type :  Desktops 	 Sales (06+13+83) :        329
PC Type :  Servers 	 Sales (06+13+83) :         41

LAB #7 - Other Useful Commands

7.1 - 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.

Create the following file:

[root@centos8 tmp]# vi expand
[root@centos8 tmp]# 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@centos8 tmp]# 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$

Important : As you can see the tabulations are shown as ^I and the end o each line as a $.

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

[root@centos8 ~]# expand expand > expand1

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

[root@centos8 ~]# 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$

Important : As you can see, the tabulations have been changed into spaces.

Command Line Switches

To do : Use the –help option of the expand command to view the command line switches.

7.2 - The unexpand Command

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@centos8 ~]# 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@centos8 ~]# unexpand -a expand1 > expand2

[root@centos8 ~]# 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$

Important : Note that the spaces have been replaced by tabulations.

Command Line Switches

To do : Use the –help option of the unexpand command to view the command line switches.

7.3 - 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@centos8 tmp]# cut -c1-7 /etc/passwd
root:x:
bin:x:1
daemon:
adm:x:3
lp:x:4:
sync:x:
shutdow
halt:x:
mail:x:
operato
games:x
ftp:x:1
nobody:
dbus:x:
systemd
systemd
tss:x:5
polkitd
unbound
libstor
cockpit
sssd:x:
setroub
sshd:x:
chrony:
tcpdump
trainee
cockpit
rngd:x:
gluster
qemu:x:
rpc:x:3
rpcuser
saslaut
radvd:x
dnsmasq

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

[root@centos8 tmp]# cut -c1-5,10-15,30- /etc/passwd
root:0:rootsh
bin:x:bin:/gin
daemo2:2:dain/nologin
adm:x:adm:/nologin
lp:x:lp:/vabin/nologin
sync:0:syncnc
shutdx:6:0::/sbin/shutdown
halt:0:haltalt
mail:12:maiail:/sbin/nologin
operax:11:0t:/sbin/nologin
games2:100:es:/sbin/nologin
ftp:x50:FTP:/sbin/nologin
nobod65534:verflow User:/:/sbin/nologin
dbus::81:Syus:/:/sbin/nologin
systeoredumstemd Core Dumper:/:/sbin/nologin
systeesolvetemd Resolver:/:/sbin/nologin
tss:x59:Acche trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
polki:998:9lkitd:/:/sbin/nologin
unbou:997:9 resolver:/etc/unbound:/sbin/nologin
libstemgmt:on account for libstoragemgmt:/var/run/lsm:/sbin/nologin
cockps:x:99 cockpit-ws:/nonexisting:/sbin/nologin
sssd:4:990:/:/sbin/nologin
setroshoot:r/lib/setroubleshoot:/sbin/nologin
sshd::74:Prted SSH:/var/empty/sshd:/sbin/nologin
chron992:98rony:/sbin/nologin
tcpdu:72:72gin
train:1000:home/trainee:/bin/bash
cockpsinstaUser for cockpit-ws instances:/nonexisting:/sbin/nologin
rngd:0:986:Generator Daemon:/var/lib/rngd:/sbin/nologin
glust:989:9aemons:/run/gluster:/sbin/nologin
qemu:7:107:bin/nologin
rpc:x32:Rpcar/lib/rpcbind:/sbin/nologin
rpcus:29:29ser:/var/lib/nfs:/sbin/nologin
saslax:988:ser:/run/saslauthd:/sbin/nologin
radvd5:75:rbin/nologin
dnsma:983:9P and DNS server:/var/lib/dnsmasq:/sbin/nologi

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

[root@centos8 tmp]# cut -d: -f2,4,6 /etc/passwd
x:0:/root
x:1:/bin
x:2:/sbin
x:4:/var/adm
x:7:/var/spool/lpd
x:0:/sbin
x:0:/sbin
x:0:/sbin
x:12:/var/spool/mail
x:0:/root
x:100:/usr/games
x:50:/var/ftp
x:65534:/
x:81:/
x:997:/
x:193:/
x:59:/dev/null
x:996:/
x:994:/etc/unbound
x:993:/var/run/lsm
x:991:/nonexisting
x:990:/
x:989:/var/lib/setroubleshoot
x:74:/var/empty/sshd
x:988:/var/lib/chrony
x:72:/
x:1000:/home/trainee
x:987:/nonexisting
x:986:/var/lib/rngd
x:985:/run/gluster
x:107:/
x:32:/var/lib/rpcbind
x:29:/var/lib/nfs
x:76:/run/saslauthd
x:75:/
x:983:/var/lib/dnsmasq
Command Line Switches

To do : Use the –help option of the cut command to view the command line switches.

7.4 - The uniq Command

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

[root@centos8 tmp]# cut -d: -f4 /etc/passwd | sort -n | uniq
0
1
2
4
7
12
29
32
50
59
72
74
75
76
81
100
107
193
983
985
986
987
988
989
990
991
993
994
996
997
1000
65534

Important: Note the use of the uniq command to remove duplicates from the list.

Command Line Switches

To do : Use the –help option of the uniq command to view the command line switches.

7.5 - 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@centos8 tmp]# cat /etc/passwd | tr "[a-z]" "[A-Z]"
ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN
DAEMON:X:2:2:DAEMON:/SBIN:/SBIN/NOLOGIN
ADM:X:3:4:ADM:/VAR/ADM:/SBIN/NOLOGIN
LP:X:4:7:LP:/VAR/SPOOL/LPD:/SBIN/NOLOGIN
SYNC:X:5:0:SYNC:/SBIN:/BIN/SYNC
SHUTDOWN:X:6:0:SHUTDOWN:/SBIN:/SBIN/SHUTDOWN
HALT:X:7:0:HALT:/SBIN:/SBIN/HALT
MAIL:X:8:12:MAIL:/VAR/SPOOL/MAIL:/SBIN/NOLOGIN
OPERATOR:X:11:0:OPERATOR:/ROOT:/SBIN/NOLOGIN
GAMES:X:12:100:GAMES:/USR/GAMES:/SBIN/NOLOGIN
FTP:X:14:50:FTP USER:/VAR/FTP:/SBIN/NOLOGIN
NOBODY:X:65534:65534:KERNEL OVERFLOW USER:/:/SBIN/NOLOGIN
DBUS:X:81:81:SYSTEM MESSAGE BUS:/:/SBIN/NOLOGIN
SYSTEMD-COREDUMP:X:999:997:SYSTEMD CORE DUMPER:/:/SBIN/NOLOGIN
SYSTEMD-RESOLVE:X:193:193:SYSTEMD RESOLVER:/:/SBIN/NOLOGIN
TSS:X:59:59:ACCOUNT USED BY THE TROUSERS PACKAGE TO SANDBOX THE TCSD DAEMON:/DEV/NULL:/SBIN/NOLOGIN
POLKITD:X:998:996:USER FOR POLKITD:/:/SBIN/NOLOGIN
UNBOUND:X:997:994:UNBOUND DNS RESOLVER:/ETC/UNBOUND:/SBIN/NOLOGIN
LIBSTORAGEMGMT:X:996:993:DAEMON ACCOUNT FOR LIBSTORAGEMGMT:/VAR/RUN/LSM:/SBIN/NOLOGIN
COCKPIT-WS:X:995:991:USER FOR COCKPIT-WS:/NONEXISTING:/SBIN/NOLOGIN
SSSD:X:994:990:USER FOR SSSD:/:/SBIN/NOLOGIN
SETROUBLESHOOT:X:993:989::/VAR/LIB/SETROUBLESHOOT:/SBIN/NOLOGIN
SSHD:X:74:74:PRIVILEGE-SEPARATED SSH:/VAR/EMPTY/SSHD:/SBIN/NOLOGIN
CHRONY:X:992:988::/VAR/LIB/CHRONY:/SBIN/NOLOGIN
TCPDUMP:X:72:72::/:/SBIN/NOLOGIN
TRAINEE:X:1000:1000:TRAINEE:/HOME/TRAINEE:/BIN/BASH
COCKPIT-WSINSTANCE:X:991:987:USER FOR COCKPIT-WS INSTANCES:/NONEXISTING:/SBIN/NOLOGIN
RNGD:X:990:986:RANDOM NUMBER GENERATOR DAEMON:/VAR/LIB/RNGD:/SBIN/NOLOGIN
GLUSTER:X:989:985:GLUSTERFS DAEMONS:/RUN/GLUSTER:/SBIN/NOLOGIN
QEMU:X:107:107:QEMU USER:/:/SBIN/NOLOGIN
RPC:X:32:32:RPCBIND DAEMON:/VAR/LIB/RPCBIND:/SBIN/NOLOGIN
RPCUSER:X:29:29:RPC SERVICE USER:/VAR/LIB/NFS:/SBIN/NOLOGIN
SASLAUTH:X:988:76:SASLAUTHD USER:/RUN/SASLAUTHD:/SBIN/NOLOGIN
RADVD:X:75:75:RADVD USER:/:/SBIN/NOLOGIN
DNSMASQ:X:983:983:DNSMASQ DHCP AND DNS SERVER:/VAR/LIB/DNSMASQ:/SBIN/NOLOGIN
Command Line Switches

To do : Use the –help option of the tr command to view the command line switches.

7.6 - The paste Command

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

[root@centos8 tmp]# paste -d: /etc/passwd /etc/shadow
root:x:0:0:root:/root:/bin/bash:root:$6$9Sa1IumuSlJc8EBg$8jGU/4xGCXy64QuBSMyKOC6/FWs41rdY5tzF5/7yHG6FRS2Y2eOJIcst1JbcvNoqMPDU4lpZ6THW97jwGuQNf1::0:99999:7:::
bin:x:1:1:bin:/bin:/sbin/nologin:bin:*:18264:0:99999:7:::
daemon:x:2:2:daemon:/sbin:/sbin/nologin:daemon:*:18264:0:99999:7:::
adm:x:3:4:adm:/var/adm:/sbin/nologin:adm:*:18264:0:99999:7:::
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin:lp:*:18264:0:99999:7:::
sync:x:5:0:sync:/sbin:/bin/sync:sync:*:18264:0:99999:7:::
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown:shutdown:*:18264:0:99999:7:::
halt:x:7:0:halt:/sbin:/sbin/halt:halt:*:18264:0:99999:7:::
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin:mail:*:18264:0:99999:7:::
operator:x:11:0:operator:/root:/sbin/nologin:operator:*:18264:0:99999:7:::
games:x:12:100:games:/usr/games:/sbin/nologin:games:*:18264:0:99999:7:::
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin:ftp:*:18264:0:99999:7:::
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin:nobody:*:18264:0:99999:7:::
dbus:x:81:81:System message bus:/:/sbin/nologin:dbus:!!:18390::::::
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin:systemd-coredump:!!:18390::::::
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin:systemd-resolve:!!:18390::::::
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin:tss:!!:18390::::::
polkitd:x:998:996:User for polkitd:/:/sbin/nologin:polkitd:!!:18390::::::
unbound:x:997:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin:unbound:!!:18390::::::
libstoragemgmt:x:996:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin:libstoragemgmt:!!:18390::::::
cockpit-ws:x:995:991:User for cockpit-ws:/nonexisting:/sbin/nologin:cockpit-ws:!!:18390::::::
sssd:x:994:990:User for sssd:/:/sbin/nologin:sssd:!!:18390::::::
setroubleshoot:x:993:989::/var/lib/setroubleshoot:/sbin/nologin:setroubleshoot:!!:18390::::::
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin:sshd:!!:18390::::::
chrony:x:992:988::/var/lib/chrony:/sbin/nologin:chrony:!!:18390::::::
tcpdump:x:72:72::/:/sbin/nologin:tcpdump:!!:18390::::::
trainee:x:1000:1000:trainee:/home/trainee:/bin/bash:trainee:$6$p4HOAHX7UAzw1nQh$VZL12Lye.mR8v1IP2e4f0PCW8DzHj2MMAaA7r2ZLoTnQN7Ziskce3bo/xTMu1bXZm5GebJjSw7.X5tABVNoJ2/::0:99999:7:::
cockpit-wsinstance:x:991:987:User for cockpit-ws instances:/nonexisting:/sbin/nologin:cockpit-wsinstance:!!:18736::::::
rngd:x:990:986:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin:rngd:!!:18736::::::
gluster:x:989:985:GlusterFS daemons:/run/gluster:/sbin/nologin:gluster:!!:18736::::::
qemu:x:107:107:qemu user:/:/sbin/nologin:qemu:!!:18736::::::
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin:rpc:!!:18736:0:99999:7:::
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin:rpcuser:!!:18736::::::
saslauth:x:988:76:Saslauthd user:/run/saslauthd:/sbin/nologin:saslauth:!!:18736::::::
radvd:x:75:75:radvd user:/:/sbin/nologin:radvd:!!:18736::::::
dnsmasq:x:983:983:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin:dnsmasq:!!:18736::::::
Command Line Switches

To do : Use the –help option of the paste command to view the command line switches.

7.7 - 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@centos8 tmp]# 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, 0.143522 s, 1.8 GB/s

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

[root@centos8 tmp]# split -b 50m /file filepart
[root@centos8 tmp]# ls -l | grep filepart
-rw-r--r--. 1 root root 52428800 Apr 20 07:14 filepartaa
-rw-r--r--. 1 root root 52428800 Apr 20 07:14 filepartab
-rw-r--r--. 1 root root 52428800 Apr 20 07:14 filepartac
-rw-r--r--. 1 root root 52428800 Apr 20 07:14 filepartad
-rw-r--r--. 1 root root 52428800 Apr 20 07:14 filepartae

Important: Note that the 5 files were created in the current working directory.

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

[root@centos8 tmp]# cat fileparta* > newfile
[root@centos8 tmp]# ls -l | grep newf
-rw-r--r--. 1 root root 262144000 Apr 20 07:15 newfile
[root@centos8 tmp]# ls -l / | grep file
-rw-r--r--.   1 root root 262144000 Apr 20 07:14 file
Command Line Switches

To do : Use the –help option of the split command to view the command line switches.

7.8 - 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@centos8 ~]# cp /etc/passwd /root

Edit the */root/passwd file as shown:

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

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

...
Linux est super!

Now compare the two files:

[root@centos8 tmp]# diff /etc/passwd /root/passwd
26,27c26
< tcpdump:x:72:72::/:/sbin/nologin
< trainee:x:1000:1000:trainee:/home/trainee:/bin/bash
---
> trainee10:x:1000:1000:trainee:/home/trainee:/bin/bash
36a36
> Linux est super!

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 26,27c26 means that line 27 needs to be changed in /etc/passwd so that is the same as line 26 in /root/passwd.

The output 36a36 means that at line 36 in /etc/passwd line 36 from /root/passwd needs to be added.

Command Line Switches

To do : Use the –help option of the diff command to view the command line switches.

7.9 - The cmp Command

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

[root@centos8 tmp]# cmp /root/passwd /etc/passwd
/root/passwd /etc/passwd differ: byte 1300, line 26

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

[root@centos8 tmp]# cmp -l /root/passwd /etc/passwd | more
cmp: EOF on /root/passwd after byte 1931
1300 162 143
1301 141 160
1302 151 144
1303 156 165
1304 145 155
1305 145 160
1306  61  72
1307  60 170
1309 170  67
1310  72  62
1311  61  72
1312  60  67
1313  60  62
1314  60  72
1316  61  57
1317  60  72
1318  60  57
1319  60 163
1320  72 142
1321 164 151
1322 162 156
1323 141  57
1324 151 156
--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

To do : Use the –help option of the cmp command to view the command line switches.

7.10 - The patch Command

La commande patch est utilisée pour appliquer des modifications à un fichier à partir d'un fichier patch qui contient les 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.

The patch command is not installed by default in RHEL/CentOS 8:

[root@centos8 ~]# dnf install patch -y

If you recall, you made some changes to the /tmp/greptest et /tmp/greptest1 files:

[root@centos8 tmp]# 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
[root@centos8 tmp]# 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@centos8 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@centos8 tmp]# cat greptest.patch
--- greptest	2021-04-20 05:23:52.710188632 -0400
+++ greptest1	2021-04-20 05:21:55.189882834 -0400
@@ -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@centos8 tmp]# patch < greptest.patch
patching file greptest

Finally, check the contents of the patched greptest file:

[root@centos8 tmp]# cat greptest
fenestrOS
fenestros
555-5555
f
.fenestros
.fe
£
Command Line Switches

To do : Use the –help option of the patch command to view the command line switches.

7.11 - 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@centos8 tmp]# strings /usr/bin/passwd | more
/lib64/ld-linux-x86-64.so.2
libuser.so.1
g_value_get_int64
is_selinux_enabled
_ITM_deregisterTMCloneTable
g_free
g_value_array_get_nth
audit_open
__gmon_start__
g_value_get_string
g_type_check_value_holds
g_value_get_long
freecon
audit_log_acct_message
_ITM_registerTMCloneTable
lu_ent_set_string
lu_ent_get_first_value_strdup
lu_error_free
lu_user_lock
lu_strerror
lu_ent_free
lu_ent_new
lu_user_modify
--More--

Print the offset within the file before each string:

[root@centos8 tmp]# strings -t d /usr/bin/passwd | more
    624 /lib64/ld-linux-x86-64.so.2
   2809 libuser.so.1
   2822 g_value_get_int64
   2840 is_selinux_enabled
   2859 _ITM_deregisterTMCloneTable
   2887 g_free
   2894 g_value_array_get_nth
   2916 audit_open
   2927 __gmon_start__
   2942 g_value_get_string
   2961 g_type_check_value_holds
   2986 g_value_get_long
   3003 freecon
   3011 audit_log_acct_message
   3034 _ITM_registerTMCloneTable
   3060 lu_ent_set_string
   3078 lu_ent_get_first_value_strdup
   3108 lu_error_free
   3122 lu_user_lock
   3135 lu_strerror
   3147 lu_ent_free
   3159 lu_ent_new
   3170 lu_user_modify
--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@centos8 tmp]# strings -t d -n 15 /usr/bin/passwd | more
    624 /lib64/ld-linux-x86-64.so.2
   2822 g_value_get_int64
   2840 is_selinux_enabled
   2859 _ITM_deregisterTMCloneTable
   2894 g_value_array_get_nth
   2942 g_value_get_string
   2961 g_type_check_value_holds
   2986 g_value_get_long
   3011 audit_log_acct_message
   3034 _ITM_registerTMCloneTable
   3060 lu_ent_set_string
   3078 lu_ent_get_first_value_strdup
   3185 lu_prompt_console
   3212 lu_user_lookup_name
   3239 lu_ent_set_long
   3281 lu_user_removepass
   3300 libgobject-2.0.so.0
   3320 libglib-2.0.so.0
   3379 poptHelpOptions
   3435 poptSetOtherOptionHelp
   3543 libpam_misc.so.0
   3584 audit_log_user_avc_message
   3611 libselinux.so.1
--More--

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

[root@centos8 tmp]# strings -f /bin/* | grep "(c)"
/bin/broadwayd:  * (c) Joel Martin (github@martintribe.org), used with permission
/bin/broadwayd: /** @license zlib.js 2012 - imaya [ https://github.com/imaya/zlib.js ] The MIT License */(function() {'use strict';var l=void 0,p=this;function q(c,d){var a=c.split("."),b=p;!(a[0]in b)&&b.execScript&&b.execScript("var "+a[0]);for(var e;a.length&&(e=a.shift());)!a.length&&d!==l?b[e]=d:b=b[e]?b[e]:b[e]={}};var r="undefined"!==typeof Uint8Array&&"undefined"!==typeof Uint16Array&&"undefined"!==typeof Uint32Array;function u(c){var d=c.length,a=0,b=Number.POSITIVE_INFINITY,e,f,g,h,k,m,s,n,t;for(n=0;n<d;++n)c[n]>a&&(a=c[n]),c[n]<b&&(b=c[n]);e=1<<a;f=new (r?Uint32Array:Array)(e);g=1;h=0;for(k=2;g<=a;){for(n=0;n<d;++n)if(c[n]===g){m=0;s=h;for(t=0;t<g;++t)m=m<<1|s&1,s>>=1;for(t=m;t<e;t+=k)f[t]=g<<16|n;++h}++g;h<<=1;k<<=1}return[f,a,b]};function v(c,d){this.g=[];this.h=32768;this.c=this.f=this.d=this.k=0;this.input=r?new Uint8Array(c):c;this.l=!1;this.i=w;this.p=!1;if(d||!(d={}))d.index&&(this.d=d.index),d.bufferSize&&(this.h=d.bufferSize),d.bufferType&&(this.i=d.bufferType),d.resize&&(this.p=d.resize);switch(this.i){case x:this.a=32768;this.b=new (r?Uint8Array:Array)(32768+this.h+258);break;case w:this.a=0;this.b=new (r?Uint8Array:Array)(this.h);this.e=this.u;this.m=this.r;this.j=this.s;break;default:throw Error("invalid inflate mode");
/bin/broadwayd: v.prototype.t=function(){for(;!this.l;){var c=y(this,3);c&1&&(this.l=!0);c>>>=1;switch(c){case 0:var d=this.input,a=this.d,b=this.b,e=this.a,f=l,g=l,h=l,k=b.length,m=l;this.c=this.f=0;f=d[a++];if(f===l)throw Error("invalid uncompressed block header: LEN (first byte)");g=f;f=d[a++];if(f===l)throw Error("invalid uncompressed block header: LEN (second byte)");g|=f<<8;f=d[a++];if(f===l)throw Error("invalid uncompressed block header: NLEN (first byte)");h=f;f=d[a++];if(f===l)throw Error("invalid uncompressed block header: NLEN (second byte)");h|=
/bin/broadwayd: function B(c){function d(a,c,b){var d,f,e,g;for(g=0;g<a;)switch(d=S(this,c),d){case 16:for(e=3+y(this,2);e--;)b[g++]=f;break;case 17:for(e=3+y(this,3);e--;)b[g++]=0;f=0;break;case 18:for(e=11+y(this,7);e--;)b[g++]=0;f=0;break;default:f=b[g++]=d}return b}var a=y(c,5)+257,b=y(c,5)+1,e=y(c,4)+4,f=new (r?Uint8Array:Array)(D.length),g,h,k,m;for(m=0;m<e;++m)f[D[m]]=y(c,3);g=u(f);h=new (r?Uint8Array:Array)(a);k=new (r?Uint8Array:Array)(b);c.j(u(d.call(c,a,g,h)),u(d.call(c,b,g,k)))}
/bin/broadwayd: v.prototype.e=function(){var c=new (r?Uint8Array:Array)(this.a-32768),d=this.a-32768,a,b,e=this.b;if(r)c.set(e.subarray(32768,c.length));else{a=0;for(b=c.length;a<b;++a)c[a]=e[a+32768]}this.g.push(c);this.k+=c.length;if(r)e.set(e.subarray(d,d+32768));else for(a=0;32768>a;++a)e[a]=e[d+a];this.a=32768;return e};
/bin/broadwayd: v.prototype.u=function(c){var d,a=this.input.length/this.d+1|0,b,e,f,g=this.input,h=this.b;c&&("number"===typeof c.o&&(a=c.o),"number"===typeof c.q&&(a+=c.q));2>a?(b=(g.length-this.d)/this.n[2],f=258*(b/2)|0,e=f<h.length?h.length+f:h.length<<1):e=h.length*a;r?(d=new Uint8Array(e),d.set(h)):d=h;return this.b=d};
/bin/btrace: # Copyright (c) 2005 Silicon Graphics, Inc.
/bin/chcat:         if len(c) > 0 and (c[0] == "+" or c[0] == "-"):
/bin/chcat:                 if len(c) > 0 and c[0] == "+":
/bin/chcat:                 if len(c) > 0 and c[0] == "-":
/bin/gprof: @(#) Copyright (c) 1983 Regents of the University of California.
/bin/lsusb.py: # Copyright (c) 2009 Kurt Garloff <garloff@suse.de>
/bin/lsusb.py: # Copyright (c) 2013 Kurt Garloff <kurt@garloff.de>
/bin/pinentry: # Copyright (c) 2006 SUSE LINUX Products GmbH, Nuernberg, Germany.
/bin/pinentry: # Copyright (c) 2009 Fedora Project
/bin/pinentry: # Copyright (c) 2014-2015 Red Hat
/bin/pkgconf: Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
/bin/pkg-config: Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
/bin/pod2usage: # Copyright (c) 1996-2000 by Bradford Appleton. All rights reserved.
/bin/pod2usage: # Copyright (c) 2001-2016 by Marek Rouchal.
/bin/qemu-img: Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers
/bin/qemu-img: Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers
/bin/qemu-io: Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers
/bin/qemu-nbd: Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers
/bin/qemu-pr-helper: Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers
/bin/rescan-scsi-bus.sh: # (c) 1998--2010 Kurt Garloff <kurt@garloff.de>, GNU GPL v2 or v3
/bin/rescan-scsi-bus.sh: # (c) 2006--2018 Hannes Reinecke, GNU GPL v2 or later
/bin/rngtest: Copyright (c) 2004 by Henrique de Moraes Holschuh
/bin/screen: Copyright (c) 2015-2017 Juergen Weigert, Alexander Naumov, Amadeusz Slawinski
/bin/screen: Copyright (c) 2010-2014 Juergen Weigert, Sadrul Habib Chowdhury
/bin/screen: Copyright (c) 2008-2009 Juergen Weigert, Michael Schroeder, Micah Cowan, Sadrul Habib Chowdhury
/bin/screen: Copyright (c) 1993-2007 Juergen Weigert, Michael Schroeder
/bin/screen: Copyright (c) 1987 Oliver Laumann
/bin/scsi-rescan: # (c) 1998--2010 Kurt Garloff <kurt@garloff.de>, GNU GPL v2 or v3
/bin/scsi-rescan: # (c) 2006--2018 Hannes Reinecke, GNU GPL v2 or later
/bin/sg_test_rwbuf: (c) Douglas Gilbert, Kurt Garloff, 2000-2007, GNU GPL
/bin/slabinfo: slabinfo 4/15/2011. (c) 2007 sgi/(c) 2011 Linux Foundation.
/bin/ssh-copy-id: # Copyright (c) 1999-2016 Philip Hands <phil@hands.com>
/bin/strace: Copyright (c) 1991-%s The strace developers <%s>.
/bin/strace-log-merge: # Copyright (c) 2012-2019 The strace developers.
/bin/systemd-analyze: hashmap_update(*uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c)) >= 0
/bin/tree: $Version: $ tree v1.7.0 (c) 1996 - 2014 by Steve Baker, Thomas Moore, Francesc Rocher, Florian Sesser, Kyosuke Tokoro $
/bin/usb-devices: # Copyright (c) 2009 Greg Kroah-Hartman <greg@kroah.com>
/bin/usb-devices: # Copyright (c) 2009 Randy Dunlap <rdunlap@xenotime.net>
/bin/usb-devices: # Copyright (c) 2009 Frans Pop <elendil@planet.nl>
/bin/vdo: # Copyright (c) 2020 Red Hat, Inc.
/bin/vdo-by-dev: # Copyright (c) 2020 Red Hat, Inc.
/bin/vdostats: # Copyright (c) 2020 Red Hat, Inc.
/bin/zip: Copyright (c) 1990-2008 Info-ZIP.  All rights reserved.
/bin/zip: Copyright (c) 1990-2008 Info-ZIP - Type '%s "-L"' for software license.
/bin/zip:     bzip2 code and library copyright (c) Julian     (See the bzip2 license for t
/bin/zipcloak: Copyright (c) 1990-2008 Info-ZIP.  All rights reserved.
/bin/zipcloak: Copyright (c) 1990-2008 Info-ZIP - Type '%s "-L"' for software license.
/bin/zipnote: Copyright (c) 1990-2008 Info-ZIP.  All rights reserved.
/bin/zipnote: Copyright (c) 1990-2008 Info-ZIP - Type '%s "-L"' for software license.
/bin/zipsplit: Copyright (c) 1990-2008 Info-ZIP.  All rights reserved.
/bin/zipsplit: Copyright (c) 1990-2008 Info-ZIP - Type '%s "-L"' for software license.
Command Line Switches

To do : Use the –help option of the strings command to view the command line switches.

7.12 - The comm Command

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

[root@centos8 tmp]# comm /etc/passwd /root/passwd
		root:x:0:0:root:/root:/bin/bash
		bin:x:1:1:bin:/bin:/sbin/nologin
		daemon:x:2:2:daemon:/sbin:/sbin/nologin
		adm:x:3:4:adm:/var/adm:/sbin/nologin
		lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
		sync:x:5:0:sync:/sbin:/bin/sync
		shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
		halt:x:7:0:halt:/sbin:/sbin/halt
		mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
		operator:x:11:0:operator:/root:/sbin/nologin
		games:x:12:100:games:/usr/games:/sbin/nologin
		ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
		nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
		dbus:x:81:81:System message bus:/:/sbin/nologin
		systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
		systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
		tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
		polkitd:x:998:996:User for polkitd:/:/sbin/nologin
		unbound:x:997:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin
		libstoragemgmt:x:996:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
		cockpit-ws:x:995:991:User for cockpit-ws:/nonexisting:/sbin/nologin
		sssd:x:994:990:User for sssd:/:/sbin/nologin
		setroubleshoot:x:993:989::/var/lib/setroubleshoot:/sbin/nologin
		sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
		chrony:x:992:988::/var/lib/chrony:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
	trainee10:x:1000:1000:trainee:/home/trainee:/bin/bash
comm: file 2 is not in sorted order
	cockpit-wsinstance:x:991:987:User for cockpit-ws instances:/nonexisting:/sbin/nologin
	rngd:x:990:986:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
	gluster:x:989:985:GlusterFS daemons:/run/gluster:/sbin/nologin
	qemu:x:107:107:qemu user:/:/sbin/nologin
	rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
	rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
	saslauth:x:988:76:Saslauthd user:/run/saslauthd:/sbin/nologin
	radvd:x:75:75:radvd user:/:/sbin/nologin
	dnsmasq:x:983:983:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin
	Linux est super!
trainee:x:1000:1000:trainee:/home/trainee:/bin/bash
comm: file 1 is not in sorted order
cockpit-wsinstance:x:991:987:User for cockpit-ws instances:/nonexisting:/sbin/nologin
rngd:x:990:986:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
gluster:x:989:985:GlusterFS daemons:/run/gluster:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
saslauth:x:988:76:Saslauthd user:/run/saslauthd:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
dnsmasq:x:983:983:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin

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.

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

[root@centos8 tmp]# comm -12 /etc/passwd /root/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
unbound:x:997:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin
libstoragemgmt:x:996:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
cockpit-ws:x:995:991:User for cockpit-ws:/nonexisting:/sbin/nologin
sssd:x:994:990:User for sssd:/:/sbin/nologin
setroubleshoot:x:993:989::/var/lib/setroubleshoot:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:992:988::/var/lib/chrony:/sbin/nologin
comm: file 2 is not in sorted order
comm: file 1 is not in sorted order
Command Line Switches

To do : Use the –help option of the comm command to view the command line switches.

7.13 - 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@centos8 tmp]# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

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

[root@centos8 tmp]# head -n 15 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin

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

[root@centos8 tmp]# head -c 150 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7[root@centos8 tmp]# 

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

[root@centos8 tmp]# head -c -150 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
unbound:x:997:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin
libstoragemgmt:x:996:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
cockpit-ws:x:995:991:User for cockpit-ws:/nonexisting:/sbin/nologin
sssd:x:994:990:User for sssd:/:/sbin/nologin
setroubleshoot:x:993:989::/var/lib/setroubleshoot:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:992:988::/var/lib/chrony:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
trainee:x:1000:1000:trainee:/home/trainee:/bin/bash
cockpit-wsinstance:x:991:987:User for cockpit-ws instances:/nonexisting:/sbin/nologin
rngd:x:990:986:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
gluster:x:989:985:GlusterFS daemons:/run/gluster:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
saslauth:x:988:76:Saslauthd us[root@centos8 tmp]# 

Both x and y can accept multipliers:

[root@centos8 tmp]# head -c 1b /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow [root@centos8 tmp]# 

[root@centos8 tmp]# head -c 512 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow [root@centos8 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

To do : Use the –help option of the head command to view the command line switches.

7.14 - 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@centos8 tmp]# tail /etc/passwd
trainee:x:1000:1000:trainee:/home/trainee:/bin/bash
cockpit-wsinstance:x:991:987:User for cockpit-ws instances:/nonexisting:/sbin/nologin
rngd:x:990:986:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
gluster:x:989:985:GlusterFS daemons:/run/gluster:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
saslauth:x:988:76:Saslauthd user:/run/saslauthd:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
dnsmasq:x:983:983:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin

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

root@centos8 tmp]# tail -n 15 /etc/passwd
sssd:x:994:990:User for sssd:/:/sbin/nologin
setroubleshoot:x:993:989::/var/lib/setroubleshoot:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:992:988::/var/lib/chrony:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
trainee:x:1000:1000:trainee:/home/trainee:/bin/bash
cockpit-wsinstance:x:991:987:User for cockpit-ws instances:/nonexisting:/sbin/nologin
rngd:x:990:986:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
gluster:x:989:985:GlusterFS daemons:/run/gluster:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
saslauth:x:988:76:Saslauthd user:/run/saslauthd:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
dnsmasq:x:983:983:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin

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

[root@centos8 tmp]# tail -c 150 /etc/passwd
er:/run/saslauthd:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
dnsmasq:x:983:983:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin

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

[root@centos8 tmp]# tail -c +150 /etc/passwd
7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
unbound:x:997:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin
libstoragemgmt:x:996:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
cockpit-ws:x:995:991:User for cockpit-ws:/nonexisting:/sbin/nologin
sssd:x:994:990:User for sssd:/:/sbin/nologin
setroubleshoot:x:993:989::/var/lib/setroubleshoot:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:992:988::/var/lib/chrony:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
trainee:x:1000:1000:trainee:/home/trainee:/bin/bash
cockpit-wsinstance:x:991:987:User for cockpit-ws instances:/nonexisting:/sbin/nologin
rngd:x:990:986:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
gluster:x:989:985:GlusterFS daemons:/run/gluster:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
saslauth:x:988:76:Saslauthd user:/run/saslauthd:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
dnsmasq:x:983:983:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin

Both x and y can accept multipliers:

[root@centos8 tmp]# tail -c 1b /etc/passwd
nstances:/nonexisting:/sbin/nologin
rngd:x:990:986:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
gluster:x:989:985:GlusterFS daemons:/run/gluster:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
saslauth:x:988:76:Saslauthd user:/run/saslauthd:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
dnsmasq:x:983:983:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin

[root@centos8 tmp]# tail -c 512 /etc/passwd
nstances:/nonexisting:/sbin/nologin
rngd:x:990:986:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
gluster:x:989:985:GlusterFS daemons:/run/gluster:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
saslauth:x:988:76:Saslauthd user:/run/saslauthd:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
dnsmasq:x:983:983:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/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@centos8 tmp]# tail -f /var/log/messages
Apr 20 06:27:53 centos8 systemd[1]: Started dnf makecache.
Apr 20 07:28:29 centos8 systemd[1]: Starting dnf makecache...
Apr 20 07:28:29 centos8 dnf[12423]: Metadata cache refreshed recently.
Apr 20 07:28:29 centos8 systemd[1]: dnf-makecache.service: Succeeded.
Apr 20 07:28:29 centos8 systemd[1]: Started dnf makecache.
Apr 20 07:50:35 centos8 systemd[1]: Started /usr/bin/systemctl start man-db-cache-update.
Apr 20 07:50:35 centos8 systemd[1]: Starting man-db-cache-update.service...
Apr 20 07:50:36 centos8 systemd[1]: man-db-cache-update.service: Succeeded.
Apr 20 07:50:36 centos8 systemd[1]: Started man-db-cache-update.service.
Apr 20 07:50:36 centos8 systemd[1]: run-r85917a32bc86476980c271609ba457fb.service: Succeeded.
^C
Command Line Switches

To do : Use the –help option of the head command to view the command line switches.

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

[root@centos8 tmp]# ifconfig ens18
ens18: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.45  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fe80::86b6:8d39:cab2:d84d  prefixlen 64  scopeid 0x20<link>
        ether 4e:b1:31:bd:5d:b2  txqueuelen 1000  (Ethernet)
        RX packets 11473  bytes 24023891 (22.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 9521  bytes 1744650 (1.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@centos8 tmp]# ifconfig ens18 | grep "inet"
        inet 10.0.2.45  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fe80::86b6:8d39:cab2:d84d  prefixlen 64  scopeid 0x20<link>

[root@centos8 tmp]# ifconfig ens18 | grep "inet" | grep -v "inet6"
        inet 10.0.2.45  netmask 255.255.255.0  broadcast 10.0.2.255

[root@centos8 tmp]# ifconfig ens18 | grep "inet" | grep -v "inet6" | tr -s " " ":"
:inet:10.0.2.45:netmask:255.255.255.0:broadcast:10.0.2.255

[root@centos8 tmp]# ifconfig ens18 | grep "inet" | grep -v "inet6" | tr -s " " ":" | cut -d: -f3
10.0.2.45

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.

LAB #9 - Use the grep, awk and sed to extract your IPv4 address from the output of ip

[root@centos8 tmp]# ip addr show ens18
2: ens18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 4e:b1:31:bd:5d:b2 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.45/24 brd 10.0.2.255 scope global noprefixroute ens18
       valid_lft forever preferred_lft forever
    inet6 fe80::86b6:8d39:cab2:d84d/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

[root@centos8 tmp]# ip addr show ens18 | grep "inet"
    inet 10.0.2.45/24 brd 10.0.2.255 scope global noprefixroute ens18
    inet6 fe80::86b6:8d39:cab2:d84d/64 scope link noprefixroute 

[root@centos8 tmp]# ip addr show ens18 | grep "inet" | grep -v "inet6"
    inet 10.0.2.45/24 brd 10.0.2.255 scope global noprefixroute ens18

[root@centos8 tmp]# ip addr show ens18 | grep "inet" | grep -v "inet6" | awk '{ print $2; }'
10.0.2.45/24

[root@centos8 tmp]# ip addr show ens18 | grep "inet" | grep -v "inet6" | awk '{ print $2; }' | sed 's/\/.*$//'
10.0.2.45

Copyright © 2022 Hugh Norris.

Menu