Ceci est une ancienne révision du document !
Table des matières
Version : 2024.01
Last updated : 2024/11/11 12:40
RH12404 - Basic Shell Commands and Text Manipulation Tools
Contents
- RH12404 - 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:
[root@redhat9 ~]# exit logout [trainee@redhat9 ~]$ stty -a speed 38400 baud; rows 27; columns 105; 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@redhat9 ~]$ date Wed Sep 25 02:45:00 PM CEST 2024
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:
[trainee@redhat9 ~]$ who trainee pts/0 2024-09-25 12:47 (10.0.2.1)
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@redhat9 ~]$ df Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 4096 0 4096 0% /dev tmpfs 3934780 0 3934780 0% /dev/shm tmpfs 1573912 9244 1564668 1% /run /dev/mapper/rhel-root 46110724 7023448 39087276 16% / /dev/sda1 1038336 406796 631540 40% /boot tmpfs 786956 52 786904 1% /run/user/42 tmpfs 786956 36 786920 1% /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@redhat9 ~]$ df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 4.0M 0 4.0M 0% /dev tmpfs 3.8G 0 3.8G 0% /dev/shm tmpfs 1.6G 9.1M 1.5G 1% /run /dev/mapper/rhel-root 44G 6.7G 38G 16% / /dev/sda1 1014M 398M 617M 40% /boot tmpfs 769M 52K 769M 1% /run/user/42 tmpfs 769M 36K 769M 1% /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@redhat9 ~]$ free total used free shared buff/cache available Mem: 7869560 845388 6607588 15324 678020 7024172 Swap: 5242876 0 5242876
The units are shown as blocks. In order to humanize the output, it is possible to use the -h switch:
[trainee@redhat9 ~]$ free -h total used free shared buff/cache available Mem: 7.5Gi 825Mi 6.3Gi 14Mi 662Mi 6.7Gi Swap: 5.0Gi 0B 5.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@redhat9 ~]$ whoami trainee
Now become the system administrator root:
[trainee@redhat9 ~]$ su - Password: fenestros
Important : Note that the password will not be visible.
Now use the whoami command again:
[root@redhat9 ~]# whoami root
Important : Note the current effective user ID is root.
Finally execute the exit command to return as trainee:
[root@redhat9 ~]# exit logout [trainee@redhat9 ~]$
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@redhat9 ~]$ 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@redhat9 ~]$ cd /tmp [trainee@redhat9 tmp]$ pwd /tmp [trainee@redhat9 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@redhat9 tmp]$ ls dbus-G7skg3Wlpv dbus-s2vBGtxTHi inode systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-colord.service-FfNuds systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-dbus-broker.service-VIpKgR systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-kdump.service-0SbYbm systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-ModemManager.service-k4DpLF systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-power-profiles-daemon.service-mIx9S5 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-rtkit-daemon.service-2gD28Z systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-switcheroo-control.service-rLbOK4 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-systemd-logind.service-uLNyfd systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-upower.service-bIpAUN
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@redhat9 tmp]$ su - Password: fenestros [root@redhat9 ~]# lsof | more COMMAND PID TID TASKCMD USER FD TYPE DEVICE SIZE/OFF NODE NAME systemd 1 root cwd DIR 253,0 235 128 / systemd 1 root rtd DIR 253,0 235 128 / systemd 1 root txt REG 253,0 98224 701526 /usr/ lib/systemd/systemd systemd 1 root mem REG 253,0 598592 68842572 /etc/ selinux/targeted/contexts/files/file_contexts.bin systemd 1 root mem REG 253,0 914360 67157969 /usr/ lib64/libm.so.6 systemd 1 root mem REG 253,0 882376 67158330 /usr/ lib64/libzstd.so.1.5.1 systemd 1 root mem REG 253,0 4487176 67224852 /usr/ lib64/libcrypto.so.3.0.7 systemd 1 root mem REG 253,0 1714208 68560752 /usr/ lib64/libp11-kit.so.0.3.1 systemd 1 root mem REG 253,0 2592552 67918617 /usr/ lib64/libc.so.6 systemd 1 root mem REG 253,0 636848 67918540 /usr/ lib64/libpcre2-8.so.0.11.0 systemd 1 root mem REG 253,0 1288512 68560763 /usr/ lib64/libgcrypt.so.20.4.0 systemd 1 root mem REG 253,0 3785712 67956161 /usr/ lib64/systemd/libsystemd-shared-252.so systemd 1 root mem REG 253,0 44784 68560751 /usr/ lib64/libffi.so.8.1.0 systemd 1 root mem REG 253,0 153600 67188275 /usr/ --More-- [q]
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@redhat9 ~]# exit logout [trainee@redhat9 tmp]$ touch test [trainee@redhat9 tmp]$ ls dbus-G7skg3Wlpv dbus-s2vBGtxTHi inode systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-colord.service-FfNuds systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-dbus-broker.service-VIpKgR systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-kdump.service-0SbYbm systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-ModemManager.service-k4DpLF systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-power-profiles-daemon.service-mIx9S5 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-rtkit-daemon.service-2gD28Z systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-switcheroo-control.service-rLbOK4 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-systemd-logind.service-uLNyfd systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-upower.service-bIpAUN 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@redhat9 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@redhat9 tmp]$ cp test ~ [trainee@redhat9 tmp]$ ls -l ~ total 4 drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Desktop drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Documents drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Downloads drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Music drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Pictures drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Public drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Templates -rw-r--r--. 1 trainee trainee 0 Sep 25 14:59 test drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Videos -rw-r--r--. 1 trainee trainee 442 Sep 25 14:24 vitext
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@redhat9 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@redhat9 tmp]$ echo "fenestros" > ~/test
Now use the file command once again to determine the file type:
[trainee@redhat9 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@redhat9 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@redhat9 tmp]$ mv ~/test . [trainee@redhat9 tmp]$ ls -l ~ total 4 drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Desktop drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Documents drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Downloads drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Music drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Pictures drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Public drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Templates drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Videos -rw-r--r--. 1 trainee trainee 442 Sep 25 14:24 vitext [trainee@redhat9 tmp]$ mv test TeSt [trainee@redhat9 tmp]$ ls -l total 4 srwxrwxrwx. 1 gdm gdm 0 Sep 25 12:30 dbus-G7skg3Wlpv srwxrwxrwx. 1 gdm gdm 0 Sep 25 12:45 dbus-s2vBGtxTHi drwxr-xr-x. 2 root root 54 Sep 25 13:10 inode drwx------. 3 root root 17 Sep 25 12:45 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-colord.service-FfNuds drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-dbus-broker.service-VIpKgR drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-kdump.service-0SbYbm drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-ModemManager.service-k4DpLF drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-power-profiles-daemon.service-mIx9S5 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-rtkit-daemon.service-2gD28Z drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-switcheroo-control.service-rLbOK4 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-systemd-logind.service-uLNyfd drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-upower.service-bIpAUN -rw-r--r--. 1 trainee trainee 10 Sep 25 15:01 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@redhat9 tmp]$ cd ~ [trainee@redhat9 ~]$ mkdir testdir [trainee@redhat9 ~]$ ls Desktop Documents Downloads Music Pictures Public Templates testdir Videos vitext
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@redhat9 ~]$ rmdir testdir [trainee@redhat9 ~]$ ls Desktop Documents Downloads Music Pictures Public Templates Videos vitext
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@redhat9 ~]$ mkdir testdir1 [trainee@redhat9 ~]$ cd /tmp [trainee@redhat9 tmp]$ echo "fenestros" > TeSt [trainee@redhat9 tmp]$ cd ~ [trainee@redhat9 ~]$ mv /tmp/TeSt ~/testdir1 [trainee@redhat9 ~]$ ls -lR testdir1/ testdir1/: total 4 -rw-r--r--. 1 trainee trainee 10 Sep 25 15:08 TeSt [trainee@redhat9 ~]$ rmdir testdir1/ rmdir: failed to remove 'testdir1/': Directory not empty [trainee@redhat9 ~]$ rm -rf testdir1 [trainee@redhat9 ~]$ ls Desktop Documents Downloads Music Pictures Public Templates Videos vitext
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@redhat9 ~]$ touch aac abc bca xyz [trainee@redhat9 ~]$ ls aac abc bca Desktop Documents Downloads Music Pictures Public Templates Videos vitext xyz [trainee@redhat9 ~]$ ls | sort aac abc bca Desktop Documents Downloads Music Pictures Public Templates Videos vitext xyz [trainee@redhat9 ~]$ ls | sort -r xyz vitext Videos Templates Public Pictures Music Downloads Documents Desktop 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@redhat9 ~]$ 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 tcpmux 1/udp # TCP port service multiplexer rje 5/tcp # Remote Job Entry rje 5/udp # Remote Job Entry --More--(0%) [q]
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@redhat9 ~]$ 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 tcpmux 1/udp # TCP port service multiplexer rje 5/tcp # Remote Job Entry rje 5/udp # Remote Job Entry /etc/services [q]
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@redhat9 ~]$ find acc find: ‘acc’: No such file or directory [trainee@redhat9 ~]$ 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@redhat9 ~]$ su - Password: fenestros [root@redhat9 ~]#
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@redhat9 ~]# ls -l /var/lib/mlocate/mlocate.db -rw-r-----. 1 root slocate 3436535 Sep 25 15:21 /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@redhat9 ~]# 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 /var/lib/mock /sysroot/ostree/deploy"
Use of these two commands is very simple:
[root@redhat9 ~]# updatedb [root@redhat9 ~]# locate aac /home/trainee/aac /usr/lib/.build-id/06/c3a6d0cdc5a9b909aac3230f36c0cdc531df73 /usr/lib/.build-id/09/896cf90eb4029fa90aacc745e15c2e4057f507 /usr/lib/.build-id/14/801c515faacd31994cac96626b4fc6431c98e3 /usr/lib/.build-id/16/6676d6a86bf3cafaac1899f6388fdb8e337b7c /usr/lib/.build-id/23/6fb54a26ac01da011289125dd0757134b23aac /usr/lib/.build-id/32/2f4ede33bea5be64a2d1d74437fed6aac08dff /usr/lib/.build-id/3c/231a016543a94a4ad2fad826bc3aacfe653af3 /usr/lib/.build-id/3d/104aacf884f203e4a4756eab74e265a10f6649 /usr/lib/.build-id/44/a9047149e3968faa7a0066bbbaac1d728b69e8 /usr/lib/.build-id/4a/9684161daa9e62b9bc5b600aacd967c50e57c4 /usr/lib/.build-id/4e/09a851a069264c701ac18fd1aac4bd656e14fa /usr/lib/.build-id/53/f01644d1fd5ae63b97390282aacf54b0b7d7ce /usr/lib/.build-id/67/3819a49f31f8bbfb5f7eb89a377094fa84eaac /usr/lib/.build-id/68/aa242c520331aacf4985d2a098c5da6614237f /usr/lib/.build-id/7e/eaac475f440b615e0355e10c2c484e129f1474 /usr/lib/.build-id/82/594874aaccba13283a7bfb590a8c2e920ed42a /usr/lib/.build-id/86/e4d1ddaebefae1e0f496ac74ca91064aac13d6 /usr/lib/.build-id/95/430b0d5c622cb1b4691ec85232b7aac072fcb4 /usr/lib/.build-id/98/39921f8f8e36cdaaac8a434953e4c54ca7dd93 /usr/lib/.build-id/9f/7ae04ba24806351a2963e65f1dfaace1f0394b /usr/lib/.build-id/b0/61a09a1e360c89eaac146ce8055ca8d45676fc /usr/lib/.build-id/bf/bf2df2e242f211ebd37ccccbdaaac6bf591bff /usr/lib/.build-id/c6/d48de21818e9eaac2ceb6273840b600a7eac7d /usr/lib/.build-id/c9/8ae396ecaacf790ba4bb73ea16f909ef212163 /usr/lib/.build-id/c9/aac0292a12c0bb393cc5457502a0338f1fb554 /usr/lib/.build-id/cc/b6a8165e506f7aacc1e633dba76719e2192fee /usr/lib/.build-id/d2/d307f71de28588b58faacd57147c666fbd38b0 /usr/lib/.build-id/d6/a427d13f7f07f818ddc87d705d0747eaaac2ba /usr/lib/.build-id/e3/8b781faac831c3d302cc7f83ee9c4f1ab85117 /usr/lib/.build-id/ea/ac093f74b0ec2597caac77f848ccf3fad2b6f5 /usr/lib/.build-id/ef/80915badd5754301e394c245c9c8e5dffaac40 /usr/lib/.build-id/f2/267eb994604adc5efff9aac163613ffeaeb77e /usr/lib/.build-id/f7/9a1addf23c6242b55cfaac4cc39a97d7ed1151 /usr/lib/.build-id/f9/79b85f31aacc55596565106c25f182f55891a7 /usr/lib/.build-id/f9/d198993d87e621b9db59273caace4344254b73 /usr/lib/.build-id/fa/d302f61d5aacdead41db2d128dd0a52eb2b411 /usr/lib/fontconfig/cache/d63f98f14a274bd69a5425fc33aaac6b-le64.cache-8 /usr/lib/modules/5.14.0-284.11.1.el9_2.x86_64/kernel/drivers/scsi/aacraid /usr/lib/modules/5.14.0-284.11.1.el9_2.x86_64/kernel/drivers/scsi/aacraid/aacraid.ko.xz /usr/lib/modules/5.14.0-427.37.1.el9_4.x86_64/kernel/drivers/scsi/aacraid /usr/lib/modules/5.14.0-427.37.1.el9_4.x86_64/kernel/drivers/scsi/aacraid/aacraid.ko.xz /usr/lib64/libfdk-aac.so.2 /usr/lib64/libfdk-aac.so.2.0.0 /usr/lib64/gstreamer-1.0/libgstfdkaac.so /usr/lib64/spa-0.2/bluez5/libspa-codec-bluez5-aac.so /usr/share/doc/fdk-aac-free /usr/share/doc/fdk-aac-free/ChangeLog /usr/share/doc/fdk-aac-free/README.fedora /usr/share/licenses/fdk-aac-free /usr/share/licenses/fdk-aac-free/NOTICE /usr/share/mime/audio/aac.xml
The default database is /var/lib/mlocate/mlocate.db :
[root@redhat9 ~]# ls -l /var/lib/mlocate/mlocate.db -rw-r-----. 1 root slocate 3436535 Sep 25 15:21 /var/lib/mlocate/mlocate.db
Important: For more information about the database format, see man 5 locatedb.
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@redhat9 ~]# whereis passwd passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man5/passwd.5.gz /usr/share/man/man1/passwd.1ossl.gz /usr/share/man/man1/passwd.1.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@redhat9 ~]# 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@redhat9 ~]# uptime 15:27:33 up 2:42, 1 user, load average: 0.00, 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@redhat9 ~]# w 15:28:01 up 2:43, 1 user, load average: 0.00, 0.00, 0.00 USER TTY LOGIN@ IDLE JCPU PCPU WHAT trainee pts/0 12:47 1.00s 0.13s 0.02s 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@redhat9 ~]# uname -a Linux redhat9.ittraining.loc 5.14.0-427.37.1.el9_4.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Sep 13 12:41:50 EDT 2024 x86_64 x86_64 x86_64 GNU/Linux [root@redhat9 ~]# uname -s Linux [root@redhat9 ~]# uname -n redhat9.ittraining.loc [root@redhat9 ~]# uname -r 5.14.0-427.37.1.el9_4.x86_64 [root@redhat9 ~]# uname -v #1 SMP PREEMPT_DYNAMIC Fri Sep 13 12:41:50 EDT 2024 [root@redhat9 ~]# uname -m x86_64 [root@redhat9 ~]# uname -p x86_64 [root@redhat9 ~]# uname -i x86_64 [root@redhat9 ~]# 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@redhat9 ~]# du -sh /* 2>/dev/null 0 /afs 0 /bin 358M /boot 0 /dev 29M /etc 4.4M /home 0 /lib 0 /lib64 0 /media 0 /mnt 0 /opt 0 /proc 2.0G /root 9.2M /run 0 /sbin 0 /srv 0 /sys 12K /tmp 4.0G /usr 368M /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@redhat9 ~]# clear [root@redhat9 ~]#
1.33 - The exit Command
This command exits the current shell:
[root@redhat9 ~]# exit logout [trainee@redhat9 ~]$
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@redhat9 ~]$ 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:
[trainee@redhat9 ~]$ su - Password: fenestros [root@redhat9 ~]# wall this is a message from root Broadcast message from root@redhat9.ittraining.loc (pts/0) (Wed Sep 25 15:31:46 this is a message from root
The wall command ignores the TZ environment variable. The time displayed on the first page is based on the system's regionalisation parameters:
[root@redhat9 ~]# date Wed Sep 25 03:33:11 PM CEST 2024
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@redhat9 ~]# seq 10 1 2 3 4 5 6 7 8 9 10 [root@redhat9 ~]# seq 10 20 10 11 12 13 14 15 16 17 18 19 20 [root@redhat9 ~]# seq 20 10 90 20 30 40 50 60 70 80 90
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 9:
[root@redhat9 ~]# which screen /usr/bin/which: no screen in (/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin) [root@redhat9 ~]# dnf makecache Updating Subscription Management repositories. Red Hat Enterprise Linux 9 for x86_64 - AppStream (RPMs) 25 kB/s | 4.5 kB 00:00 Red Hat Enterprise Linux 9 for x86_64 - BaseOS (RPMs) 25 kB/s | 4.1 kB 00:00 Metadata cache created. [root@redhat9 ~]# dnf install screen -y Updating Subscription Management repositories. Last metadata expiration check: 0:00:49 ago on Wed 25 Sep 2024 03:35:01 PM CEST. No match for argument: screen Error: Unable to find a match: screen [root@redhat9 ~]# subscription-manager repos --enable codeready-builder-for-rhel-9-$(arch)-rpms Repository 'codeready-builder-for-rhel-9-x86_64-rpms' is enabled for this system. [root@redhat9 ~]# dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm Updating Subscription Management repositories. Red Hat CodeReady Linux Builder for RHEL 9 x86_64 (RPMs) 5.0 MB/s | 9.1 MB 00:01 Last metadata expiration check: 0:00:01 ago on Wed 25 Sep 2024 03:39:17 PM CEST. epel-release-latest-9.noarch.rpm 158 kB/s | 18 kB 00:00 Dependencies resolved. ========================================================================================================= Package Architecture Version Repository Size ========================================================================================================= Installing: epel-release noarch 9-8.el9 @commandline 18 k Transaction Summary ========================================================================================================= Install 1 Package Total size: 18 k Installed size: 26 k Is this ok [y/N]: y Downloading Packages: Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : epel-release-9-8.el9.noarch 1/1 Running scriptlet: epel-release-9-8.el9.noarch 1/1 Many EPEL packages require the CodeReady Builder (CRB) repository. It is recommended that you run /usr/bin/crb enable to enable the CRB repository. Verifying : epel-release-9-8.el9.noarch 1/1 Installed products updated. Installed: epel-release-9-8.el9.noarch Complete! [root@redhat9 ~]# dnf install screen Updating Subscription Management repositories. Extra Packages for Enterprise Linux 9 - Next - x86_64 179 kB/s | 276 kB 00:01 Last metadata expiration check: 0:00:01 ago on Wed 25 Sep 2024 03:43:49 PM CEST. Dependencies resolved. ========================================================================================================= Package Architecture Version Repository Size ========================================================================================================= Installing: screen x86_64 4.8.0-6.el9 epel 649 k Transaction Summary ========================================================================================================= Install 1 Package Total download size: 649 k Installed size: 957 k Is this ok [y/N]: y Downloading Packages: screen-4.8.0-6.el9.x86_64.rpm 1.0 MB/s | 649 kB 00:00 --------------------------------------------------------------------------------------------------------- Total 875 kB/s | 649 kB 00:00 Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Running scriptlet: screen-4.8.0-6.el9.x86_64 1/1 Installing : screen-4.8.0-6.el9.x86_64 1/1 Running scriptlet: screen-4.8.0-6.el9.x86_64 1/1 Verifying : screen-4.8.0-6.el9.x86_64 1/1 Installed products updated. Installed: screen-4.8.0-6.el9.x86_64 Complete!
Create a session with screen:
[root@redhat9 ~]# screen -S mysession
Now press the CTRL and A keys, release the A key and press the C key in order to create a second nested screen.
To return to the first screen, use the CTRL A A keys. This allows you to toggle between the last two screens used.
To see the status of all active screens, use the screen -ls command:
[root@redhat9 ~]# screen -ls There is a screen on: 3103.mysession (Attached) 1 Socket in /run/screen/S-root.
Now enter the following commands:
[root@redhat9 ~]# sleep 999 & [1] 3153 [root@redhat9 ~]# jobs -l [1]+ 3153 Running sleep 999 & [root@redhat9 ~]#
In order to detach the current screen press the CTRL and A keys, release the A key and press the D key:
[root@redhat9 ~]# screen -S mysession [detached from 3103.mysession] [root@redhat9 ~]#
To re-attach the screen, execute the following command:
[root@redhat9 ~]# screen -r
Using the jobs command, check if the process created by the sleep command is still running:
[root@redhat9 ~]# jobs -l [1]+ 3153 Running sleep 999 & [root@redhat9 ~]#
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@redhat9 ~]# screen -S mysession [detached from 3103.mysession] [root@redhat9 ~]#
Now create a new, non-nested screen:
[root@redhat9 ~]# screen -S mysession1
Use the screen -ls command to see what has happened:
[root@redhat9 ~]# screen -ls There are screens on: 3181.mysession1 (Attached) 3103.mysession (Detached) 2 Sockets in /run/screen/S-root.
Note, however, that this screen 0 is not the same as the previous screen 0 in which you executed the sleep command:
[root@redhat9 ~]# jobs -l [root@redhat9 ~]#
To re-attach a specific screen, reference it by it's number:
[root@redhat9 ~]# screen -r 3103 [root@redhat9 ~]# screen -ls There are screens on: 3181.mysession1 (Attached) 3103.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@redhat9 ~]# screen -S mysession1 [detached from 3181.mysession1] [root@redhat9 ~]#
Now check which screen you are connected to:
[root@redhat9 ~]# screen -ls There are screens on: 3181.mysession1 (Detached) 3103.mysession (Attached) 2 Sockets in /run/screen/S-root.
Finally, kill the two sessions:
[root@redhat9 ~]# screen -ls There are screens on: 3181.mysession1 (Detached) 3103.mysession (Attached) 2 Sockets in /run/screen/S-root.
Tuez maintenant les deux sessions :
[root@redhat9 ~]# screen -XS 3181 quit [root@redhat9 ~]# screen -ls There is a screen on: 3103.mysession (Detached) 1 Socket in /run/screen/S-root. [root@redhat9 ~]# screen -XS 3103 quit [root@redhat9 ~]# 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@redhat9 ~]# ls -lai /tmp total 12 33554561 drwxrwxrwt. 16 root root 4096 Sep 25 15:54 . 128 dr-xr-xr-x. 18 root root 235 Oct 19 2023 .. 33786429 srwxrwxrwx. 1 gdm gdm 0 Sep 25 12:30 dbus-G7skg3Wlpv 33625153 srwxrwxrwx. 1 gdm gdm 0 Sep 25 12:45 dbus-s2vBGtxTHi 33554609 drwxrwxrwt. 2 root root 6 Sep 25 12:30 .font-unix 34654589 drwxrwxrwt. 2 root root 18 Sep 25 12:44 .ICE-unix 33625083 drwxr-xr-x. 2 root root 54 Sep 25 13:10 inode 100664689 drwx------. 3 root root 17 Sep 25 12:45 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-colord.service-FfNuds 10601 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-dbus-broker.service-VIpKgR 67157623 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-kdump.service-0SbYbm 67157622 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-ModemManager.service-k4DpLF 67155430 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-power-profiles-daemon.service-mIx9S5 67155431 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-rtkit-daemon.service-2gD28Z 67157618 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-switcheroo-control.service-rLbOK4 67157619 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-systemd-logind.service-uLNyfd 67157620 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-upower.service-bIpAUN 33625086 -r--r--r--. 1 gdm gdm 11 Sep 25 12:45 .X1024-lock 33625152 -r--r--r--. 1 gdm gdm 11 Sep 25 12:45 .X1025-lock 2074740 drwxrwxrwt. 2 root root 32 Sep 25 12:45 .X11-unix 10599 drwxrwxrwt. 2 root root 6 Sep 25 12:30 .XIM-unix [root@redhat9 ~]# ls -ali /tmp total 12 33554561 drwxrwxrwt. 16 root root 4096 Sep 25 15:54 . 128 dr-xr-xr-x. 18 root root 235 Oct 19 2023 .. 33786429 srwxrwxrwx. 1 gdm gdm 0 Sep 25 12:30 dbus-G7skg3Wlpv 33625153 srwxrwxrwx. 1 gdm gdm 0 Sep 25 12:45 dbus-s2vBGtxTHi 33554609 drwxrwxrwt. 2 root root 6 Sep 25 12:30 .font-unix 34654589 drwxrwxrwt. 2 root root 18 Sep 25 12:44 .ICE-unix 33625083 drwxr-xr-x. 2 root root 54 Sep 25 13:10 inode 100664689 drwx------. 3 root root 17 Sep 25 12:45 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-colord.service-FfNuds 10601 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-dbus-broker.service-VIpKgR 67157623 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-kdump.service-0SbYbm 67157622 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-ModemManager.service-k4DpLF 67155430 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-power-profiles-daemon.service-mIx9S5 67155431 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-rtkit-daemon.service-2gD28Z 67157618 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-switcheroo-control.service-rLbOK4 67157619 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-systemd-logind.service-uLNyfd 67157620 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-upower.service-bIpAUN 33625086 -r--r--r--. 1 gdm gdm 11 Sep 25 12:45 .X1024-lock 33625152 -r--r--r--. 1 gdm gdm 11 Sep 25 12:45 .X1025-lock 2074740 drwxrwxrwt. 2 root root 32 Sep 25 12:45 .X11-unix 10599 drwxrwxrwt. 2 root root 6 Sep 25 12:30 .XIM-unix [root@redhat9 ~]# ls -ial /tmp total 12 33554561 drwxrwxrwt. 16 root root 4096 Sep 25 15:54 . 128 dr-xr-xr-x. 18 root root 235 Oct 19 2023 .. 33786429 srwxrwxrwx. 1 gdm gdm 0 Sep 25 12:30 dbus-G7skg3Wlpv 33625153 srwxrwxrwx. 1 gdm gdm 0 Sep 25 12:45 dbus-s2vBGtxTHi 33554609 drwxrwxrwt. 2 root root 6 Sep 25 12:30 .font-unix 34654589 drwxrwxrwt. 2 root root 18 Sep 25 12:44 .ICE-unix 33625083 drwxr-xr-x. 2 root root 54 Sep 25 13:10 inode 100664689 drwx------. 3 root root 17 Sep 25 12:45 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-colord.service-FfNuds 10601 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-dbus-broker.service-VIpKgR 67157623 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-kdump.service-0SbYbm 67157622 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-ModemManager.service-k4DpLF 67155430 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-power-profiles-daemon.service-mIx9S5 67155431 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-rtkit-daemon.service-2gD28Z 67157618 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-switcheroo-control.service-rLbOK4 67157619 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-systemd-logind.service-uLNyfd 67157620 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-upower.service-bIpAUN 33625086 -r--r--r--. 1 gdm gdm 11 Sep 25 12:45 .X1024-lock 33625152 -r--r--r--. 1 gdm gdm 11 Sep 25 12:45 .X1025-lock 2074740 drwxrwxrwt. 2 root root 32 Sep 25 12:45 .X11-unix 10599 drwxrwxrwt. 2 root root 6 Sep 25 12:30 .XIM-unix
However ls -l –all –inode cannot be written ls -l –allinode:
[root@redhat9 ~]# ls -l --all --inode /tmp total 12 33554561 drwxrwxrwt. 16 root root 4096 Sep 25 15:54 . 128 dr-xr-xr-x. 18 root root 235 Oct 19 2023 .. 33786429 srwxrwxrwx. 1 gdm gdm 0 Sep 25 12:30 dbus-G7skg3Wlpv 33625153 srwxrwxrwx. 1 gdm gdm 0 Sep 25 12:45 dbus-s2vBGtxTHi 33554609 drwxrwxrwt. 2 root root 6 Sep 25 12:30 .font-unix 34654589 drwxrwxrwt. 2 root root 18 Sep 25 12:44 .ICE-unix 33625083 drwxr-xr-x. 2 root root 54 Sep 25 13:10 inode 100664689 drwx------. 3 root root 17 Sep 25 12:45 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-colord.service-FfNuds 10601 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-dbus-broker.service-VIpKgR 67157623 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-kdump.service-0SbYbm 67157622 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-ModemManager.service-k4DpLF 67155430 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-power-profiles-daemon.service-mIx9S5 67155431 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-rtkit-daemon.service-2gD28Z 67157618 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-switcheroo-control.service-rLbOK4 67157619 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-systemd-logind.service-uLNyfd 67157620 drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-upower.service-bIpAUN 33625086 -r--r--r--. 1 gdm gdm 11 Sep 25 12:45 .X1024-lock 33625152 -r--r--r--. 1 gdm gdm 11 Sep 25 12:45 .X1025-lock 2074740 drwxrwxrwt. 2 root root 32 Sep 25 12:45 .X11-unix 10599 drwxrwxrwt. 2 root root 6 Sep 25 12:30 .XIM-unix [root@redhat9 ~]# 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@redhat9 ~]# cd /tmp [root@redhat9 tmp]# vi greptest [root@redhat9 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@redhat9 ~]# 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@redhat9 ~]# 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@redhat9 ~]# 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@redhat9 ~]# grep '^.$' /tmp/greptest f £
To search for a line containing a special character such as ., that character needs to be preceded by \:
[root@redhat9 ~]# grep '^\.' /tmp/greptest .fenestros .fe
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@redhat9 tmp]# vi greptest [root@redhat9 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@redhat9 ~]# 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@redhat9 ~]# egrep -v '^(#|$)' /tmp/greptest > /tmp/greptest1 [root@redhat9 ~]# 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@redhat9 tmp]# vi greptest [root@redhat9 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@redhat9 ~]# 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@redhat9 ~]# 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@redhat9 ~]# 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@redhat9 ~]# 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 --More--
Now display the same file without any commented lines:
[root@redhat9 ~]# 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 --More--
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@redhat9 ~]# 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:
[root@redhat9 ~]# 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@redhat9 ~]# sed -n '/^#/!w /tmp/sedtest' /etc/services [root@redhat9 ~]# 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 --More--(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@redhat9 ~]# echo "user1,user2,user3" > /tmp/sedtest1 [root@redhat9 ~]# 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@redhat9 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@redhat9 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@redhat9 tmp]# cat > scriptawk BEGIN { print "List of mounted filesystems"} {print $0} END { print "====================================="} [^D]
Now apply the awk script to /etc/fstab :
[root@redhat9 tmp]# awk -f scriptawk /etc/mtab List of mounted filesystems 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@redhat9 tmp]# vi sales.txt [root@redhat9 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@redhat9 tmp]# vi sales.awk [root@redhat9 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@redhat9 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@redhat9 tmp]# vi expand [root@redhat9 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@redhat9 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 of each line as a $.
Now use the expand command to convert the tabulations into spaces and send the result to the expand1 file:
[root@redhat9 ~]# expand expand > expand1
View the resulting expand1 file with the cat command and the -vet switches:
[root@redhat9 ~]# 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@redhat9 ~]# 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@redhat9 ~]# unexpand -a expand1 > expand2 [root@redhat9 ~]# 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@redhat9 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: systemd dbus:x: polkitd avahi:x tss:x:5 colord: clevis: rtkit:x sssd:x: geoclue libstor systemd setroub pipewir flatpak gdm:x:4 cockpit cockpit gnome-i sshd:x: chrony: dnsmasq tcpdump trainee
In order to select columns 1 to 5, columns 10 to 15 and columns 30 and higher, us the following command:
[root@redhat9 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 systeoredumstemd Core Dumper:/:/sbin/nologin dbus::81:Syus:/:/sbin/nologin polki:998:9lkitd:/:/sbin/nologin avahi0:70:ASD Stack:/var/run/avahi-daemon:/sbin/nologin tss:x59:AccTPM access:/dev/null:/sbin/nologin color997:99ord:/var/lib/colord:/sbin/nologin clevi996:99ption Framework unprivileged user:/var/cache/clevis:/usr/sbin/nologin rtkit72:172proc:/sbin/nologin sssd:5:991:/:/sbin/nologin geocl:994:9oclue:/var/lib/geoclue:/sbin/nologin libstemgmt:on account for libstoragemgmt:/:/usr/sbin/nologin systeom:x:9 Userspace OOM Killer:/:/usr/sbin/nologin setroshoot:nux troubleshoot server:/var/lib/setroubleshoot:/sbin/nologin pipewx:985:ystem Daemon:/var/run/pipewire:/sbin/nologin flatp:984:9atpak system helper:/:/sbin/nologin gdm:x42::/vin/nologin cockps:x:98 cockpit web service:/nonexisting:/sbin/nologin cockpsinstaUser for cockpit-ws instances:/nonexisting:/sbin/nologin gnometial-s::/run/gnome-initial-setup/:/sbin/nologin sshd::74:Prted SSH:/usr/share/empty.sshd:/sbin/nologin chron980:97m user:/var/lib/chrony:/sbin/nologin dnsma:979:9P and DNS server:/var/lib/dnsmasq:/sbin/nologin tcpdu:72:72gin train:1000:home/trainee:/bin/bash
In order to select the 2nd, 4th and 6th column, use the following command:
[root@redhat9 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:997:/ x:81:/ x:996:/ x:70:/var/run/avahi-daemon x:59:/dev/null x:993:/var/lib/colord x:992:/var/cache/clevis x:172:/proc x:991:/ x:990:/var/lib/geoclue x:988:/ x:987:/ x:986:/var/lib/setroubleshoot x:984:/var/run/pipewire x:983:/ x:42:/var/lib/gdm x:982:/nonexisting x:981:/nonexisting x:980:/run/gnome-initial-setup/ x:74:/usr/share/empty.sshd x:979:/var/lib/chrony x:978:/var/lib/dnsmasq x:72:/ x:1000:/home/trainee
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@redhat9 tmp]# cut -d: -f4 /etc/passwd | sort -n | uniq 0 1 2 4 7 12 42 50 59 70 72 74 81 100 172 978 979 980 981 982 983 984 986 987 988 990 991 992 993 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@redhat9 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 SYSTEMD-COREDUMP:X:999:997:SYSTEMD CORE DUMPER:/:/SBIN/NOLOGIN DBUS:X:81:81:SYSTEM MESSAGE BUS:/:/SBIN/NOLOGIN POLKITD:X:998:996:USER FOR POLKITD:/:/SBIN/NOLOGIN AVAHI:X:70:70:AVAHI MDNS/DNS-SD STACK:/VAR/RUN/AVAHI-DAEMON:/SBIN/NOLOGIN TSS:X:59:59:ACCOUNT USED FOR TPM ACCESS:/DEV/NULL:/SBIN/NOLOGIN COLORD:X:997:993:USER FOR COLORD:/VAR/LIB/COLORD:/SBIN/NOLOGIN CLEVIS:X:996:992:CLEVIS DECRYPTION FRAMEWORK UNPRIVILEGED USER:/VAR/CACHE/CLEVIS:/USR/SBIN/NOLOGIN RTKIT:X:172:172:REALTIMEKIT:/PROC:/SBIN/NOLOGIN SSSD:X:995:991:USER FOR SSSD:/:/SBIN/NOLOGIN GEOCLUE:X:994:990:USER FOR GEOCLUE:/VAR/LIB/GEOCLUE:/SBIN/NOLOGIN LIBSTORAGEMGMT:X:988:988:DAEMON ACCOUNT FOR LIBSTORAGEMGMT:/:/USR/SBIN/NOLOGIN SYSTEMD-OOM:X:987:987:SYSTEMD USERSPACE OOM KILLER:/:/USR/SBIN/NOLOGIN SETROUBLESHOOT:X:986:986:SELINUX TROUBLESHOOT SERVER:/VAR/LIB/SETROUBLESHOOT:/SBIN/NOLOGIN PIPEWIRE:X:985:984:PIPEWIRE SYSTEM DAEMON:/VAR/RUN/PIPEWIRE:/SBIN/NOLOGIN FLATPAK:X:984:983:USER FOR FLATPAK SYSTEM HELPER:/:/SBIN/NOLOGIN GDM:X:42:42::/VAR/LIB/GDM:/SBIN/NOLOGIN COCKPIT-WS:X:983:982:USER FOR COCKPIT WEB SERVICE:/NONEXISTING:/SBIN/NOLOGIN COCKPIT-WSINSTANCE:X:982:981:USER FOR COCKPIT-WS INSTANCES:/NONEXISTING:/SBIN/NOLOGIN GNOME-INITIAL-SETUP:X:981:980::/RUN/GNOME-INITIAL-SETUP/:/SBIN/NOLOGIN SSHD:X:74:74:PRIVILEGE-SEPARATED SSH:/USR/SHARE/EMPTY.SSHD:/SBIN/NOLOGIN CHRONY:X:980:979:CHRONY SYSTEM USER:/VAR/LIB/CHRONY:/SBIN/NOLOGIN DNSMASQ:X:979:978:DNSMASQ DHCP AND DNS SERVER:/VAR/LIB/DNSMASQ:/SBIN/NOLOGIN TCPDUMP:X:72:72::/:/SBIN/NOLOGIN TRAINEE:X:1000:1000:TRAINEE:/HOME/TRAINEE:/BIN/BASH
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@redhat9 tmp]# paste -d: /etc/passwd /etc/shadow root:x:0:0:root:/root:/bin/bash:root:$6$AbCPA3HFsB/NDBkA$q2T8XLo83bPWCid/WHo.i0m9dgjdfQWiZAkqD/ajOjZ8gHdKFYX5y8kuTvIYY/qMjmu9beCk3BZV8ewL/Q15D1::0:99999:7::: bin:x:1:1:bin:/bin:/sbin/nologin:bin:*:19347:0:99999:7::: daemon:x:2:2:daemon:/sbin:/sbin/nologin:daemon:*:19347:0:99999:7::: adm:x:3:4:adm:/var/adm:/sbin/nologin:adm:*:19347:0:99999:7::: lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin:lp:*:19347:0:99999:7::: sync:x:5:0:sync:/sbin:/bin/sync:sync:*:19347:0:99999:7::: shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown:shutdown:*:19347:0:99999:7::: halt:x:7:0:halt:/sbin:/sbin/halt:halt:*:19347:0:99999:7::: mail:x:8:12:mail:/var/spool/mail:/sbin/nologin:mail:*:19347:0:99999:7::: operator:x:11:0:operator:/root:/sbin/nologin:operator:*:19347:0:99999:7::: games:x:12:100:games:/usr/games:/sbin/nologin:games:*:19347:0:99999:7::: ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin:ftp:*:19347:0:99999:7::: nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin:nobody:*:19347:0:99999:7::: systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin:systemd-coredump:!!:19649:::::: dbus:x:81:81:System message bus:/:/sbin/nologin:dbus:!!:19649:::::: polkitd:x:998:996:User for polkitd:/:/sbin/nologin:polkitd:!!:19649:::::: avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin:avahi:!!:19649:::::: tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin:tss:!!:19649:::::: colord:x:997:993:User for colord:/var/lib/colord:/sbin/nologin:colord:!!:19649:::::: clevis:x:996:992:Clevis Decryption Framework unprivileged user:/var/cache/clevis:/usr/sbin/nologin:clevis:!!:19649:::::: rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin:rtkit:!!:19649:::::: sssd:x:995:991:User for sssd:/:/sbin/nologin:sssd:!!:19649:::::: geoclue:x:994:990:User for geoclue:/var/lib/geoclue:/sbin/nologin:geoclue:!!:19649:::::: libstoragemgmt:x:988:988:daemon account for libstoragemgmt:/:/usr/sbin/nologin:libstoragemgmt:!*:19649:::::: systemd-oom:x:987:987:systemd Userspace OOM Killer:/:/usr/sbin/nologin:systemd-oom:!*:19649:::::: setroubleshoot:x:986:986:SELinux troubleshoot server:/var/lib/setroubleshoot:/sbin/nologin:setroubleshoot:!!:19649:::::: pipewire:x:985:984:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin:pipewire:!!:19649:::::: flatpak:x:984:983:User for flatpak system helper:/:/sbin/nologin:flatpak:!!:19649:::::: gdm:x:42:42::/var/lib/gdm:/sbin/nologin:gdm:!!:19649:::::: cockpit-ws:x:983:982:User for cockpit web service:/nonexisting:/sbin/nologin:cockpit-ws:!!:19649:::::: cockpit-wsinstance:x:982:981:User for cockpit-ws instances:/nonexisting:/sbin/nologin:cockpit-wsinstance:!!:19649:::::: gnome-initial-setup:x:981:980::/run/gnome-initial-setup/:/sbin/nologin:gnome-initial-setup:!!:19649:::::: sshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/sbin/nologin:sshd:!!:19649:::::: chrony:x:980:979:chrony system user:/var/lib/chrony:/sbin/nologin:chrony:!!:19649:::::: dnsmasq:x:979:978:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin:dnsmasq:!!:19649:::::: tcpdump:x:72:72::/:/sbin/nologin:tcpdump:!!:19649:::::: trainee:x:1000:1000:trainee:/home/trainee:/bin/bash:trainee:$6$RTR0r5su3SinU2DK$dt/TI6LByO3SKM04nopxI33O7.eE62rPQ0DlO2HRH2PUtPM4c1pvh3koznv6nE6Z0oCoM0Fq7IUdt8cbjXUMh0::0:99999:7:::
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@redhat9 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.132123 s, 2.0 GB/s
Now use the split command to divide the file into 5 smaller files each of 50:
[root@redhat9 tmp]# split -b 50m /file filepart [root@redhat9 tmp]# ls -l | grep filepart -rw-r--r--. 1 root root 52428800 Sep 25 16:17 filepartaa -rw-r--r--. 1 root root 52428800 Sep 25 16:17 filepartab -rw-r--r--. 1 root root 52428800 Sep 25 16:17 filepartac -rw-r--r--. 1 root root 52428800 Sep 25 16:17 filepartad -rw-r--r--. 1 root root 52428800 Sep 25 16:17 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@redhat9 tmp]# cat fileparta* > newfile [root@redhat9 tmp]# ls -l | grep newf -rw-r--r--. 1 root root 262144000 Sep 25 16:18 newfile [root@redhat9 tmp]# ls -l / | grep file -rw-r--r--. 1 root root 262144000 Sep 25 16:17 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@redhat9 ~]# 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@redhat9 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@redhat9 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@redhat9 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 9:
[root@redhat9 ~]# dnf install patch -y
If you recall, you made some changes to the /tmp/greptest et /tmp/greptest1 files:
[root@redhat9 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@redhat9 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@redhat9 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@redhat9 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@redhat9 tmp]# patch < greptest.patch patching file greptest
Finally, check the contents of the patched greptest file:
[root@redhat9 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@redhat9 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@redhat9 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@redhat9 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@redhat9 tmp]# strings -f /bin/* | grep "(c)" /bin/btrace: # Copyright (c) 2005 Silicon Graphics, Inc. /bin/buildah: 0,1,2,3,4,5,6,7,8,9,(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)(m)(n)(o)(p)(q)(r)(s)(t)(u)(v)(w)(x)(y)(z) /bin/buildah: B!!B!?B..B0,B0.B1,B1.B10B11B12B13B14B15B16B17B18B19B2,B2.B20B21B22B23B24B25B26B27B28B29B3,B3.B30B31B32B33B34B35B36B37B38B39B4,B4.B40B41B42B43B44B45B46B47B48B49B5,B5.B50B6,B6.B7,B7.B8,B8.B9,B9.B==B?!B??BAUBBqBCDBDJBDZBDzBGBBGyBHPBHVBHgBHzBIIBIJBIUBIVBIXBKBBKKBKMBLJBLjBMBBMCBMDBMRBMVBMWBNJBNjBNoBPHBPRBPaBRsBSDBSMBSSBSvBTMBVIBWCBWZBWbBXIBccBcdBcmBdBBdaBdlBdmBdzBeVBffBfiBflBfmBhaBiiBijBinBivBixBkABkVBkWBkgBklBkmBktBljBlmBlnBlxBm2Bm3BmABmVBmWBmbBmgBmlBmmBmsBnABnFBnVBnWBnjBnmBnsBoVBpABpFBpVBpWBpcBpsBsrBstBviBxiC(1)C(2)C(3)C(4)C(5)C(6)C(7)C(8)C(9)C(A)C(B)C(C)C(D)C(E)C(F)C(G)C(H)C(I)C(J)C(K)C(L)C(M)C(N)C(O)C(P)C(Q)C(R)C(S)C(T)C(U)C(V)C(W)C(X)C(Y)C(Z)C(a)C(b)C(c)C(d)C(e)C(f)C(g)C(h)C(i)C(j)C(k)C(l)C(m)C(n)C(o)C(p)C(q)C(r)C(s)C(t)C(u)C(v)C(w)C(x)C(y)C(z)C...C10.C11.C12.C13.C14.C15.C16.C17.C18.C19.C20.C::=C===CCo.CFAXCGHzCGPaCIIICLTDCL /bin/cdda-player: (c) 1997-98 Gerd Knorr <kraxel@goldbach.in-berlin.de> /bin/cdda-player: (c) 2005-2006, 2017 Rocky Bernstein <rocky@gnu.org> /bin/cd-drive: Copyright (c) 2003-2005, 2007-2008, 2011-2015, 2017 R. Bernstein /bin/cd-info: Copyright (c) 2003-2005, 2007-2008, 2011-2015, 2017 R. Bernstein /bin/cd-read: Copyright (c) 2003-2005, 2007-2008, 2011-2015, 2017 R. Bernstein /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/clevis: # Copyright (c) 2017 Red Hat, Inc. /bin/clevis-decrypt: # Copyright (c) 2017 Red Hat, Inc. /bin/clevis-decrypt-null: # Copyright (c) 2017 Red Hat, Inc. /bin/clevis-decrypt-tang: # Copyright (c) 2017 Red Hat, Inc. /bin/clevis-decrypt-tpm2: # Copyright (c) 2017 Red Hat, Inc. /bin/clevis-encrypt-null: # Copyright (c) 2017 Red Hat, Inc. /bin/clevis-encrypt-tang: # Copyright (c) 2017 Red Hat, Inc. /bin/clevis-encrypt-tpm2: # Copyright (c) 2017 Red Hat, Inc. /bin/clevis-luks-bind: # Copyright (c) 2016 Red Hat, Inc. /bin/clevis-luks-common-functions: # Copyright (c) 2019 Red Hat, Inc. /bin/clevis-luks-edit: # Copyright (c) 2020 Red Hat, Inc. /bin/clevis-luks-list: # Copyright (c) 2017-2019 Red Hat, Inc. /bin/clevis-luks-pass: # Copyright (c) 2019 Red Hat, Inc. /bin/clevis-luks-regen: # Copyright (c) 2020 Red Hat, Inc. /bin/clevis-luks-report: # Copyright (c) 2018, 2020 Red Hat, Inc. /bin/clevis-luks-unbind: # Copyright (c) 2017 Red Hat, Inc. /bin/clevis-luks-unlock: # Copyright (c) 2016 Red Hat, Inc. /bin/diffpp: # Copyright (c) 1996-1998 Markku Rossi /bin/gnome-control-center: NM_IS_REMOTE_CONNECTION (c) /bin/ibus-setup: # Copyright (c) 2007-2010 Peng Huang <shawn.p.huang@gmail.com> /bin/ibus-setup: # Copyright (c) 2018-2019 Takao Fujiwara <takao.fujiwara1@gmail.com> /bin/ibus-setup: # Copyright (c) 2007-2018 Red Hat, Inc. /bin/iso-info: Copyright (c) 2003-2005, 2007-2008, 2011-2015, 2017 R. Bernstein /bin/iso-read: Copyright (c) 2003-2005, 2007-2008, 2011-2015, 2017 R. Bernstein /bin/itstool: # Copyright (c) 2010-2018 Shaun McCance <shaunm@gnome.org> /bin/lsusb.py: # Copyright (c) 2009 Kurt Garloff <garloff@suse.de> /bin/lsusb.py: # Copyright (c) 2013,2018 Kurt Garloff <kurt@garloff.de> /bin/mmc-tool: Copyright (c) 2003-2005, 2007-2008, 2011-2015, 2017 R. Bernstein /bin/orca: __copyright__ = "Copyright (c) 2010-2012 The Orca Team" \ /bin/orca: "Copyright (c) 2012 Igalia, S.L." /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, 2019, 2020 /bin/pod2usage: # Copyright (c) 1996-2000 by Bradford Appleton. All rights reserved. /bin/pod2usage: # Copyright (c) 2001-2016 by Marek Rouchal. /bin/podman: 0,1,2,3,4,5,6,7,8,9,(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)(m)(n)(o)(p)(q)(r)(s)(t)(u)(v)(w)(x)(y)(z) /bin/podman: B!!B!?B..B0,B0.B1,B1.B10B11B12B13B14B15B16B17B18B19B2,B2.B20B21B22B23B24B25B26B27B28B29B3,B3.B30B31B32B33B34B35B36B37B38B39B4,B4.B40B41B42B43B44B45B46B47B48B49B5,B5.B50B6,B6.B7,B7.B8,B8.B9,B9.B==B?!B??BAUBBqBCDBDJBDZBDzBGBBGyBHPBHVBHgBHzBIIBIJBIUBIVBIXBKBBKKBKMBLJBLjBMBBMCBMDBMRBMVBMWBNJBNjBNoBPHBPRBPaBRsBSDBSMBSSBSvBTMBVIBWCBWZBWbBXIBccBcdBcmBdBBdaBdlBdmBdzBeVBffBfiBflBfmBhaBiiBijBinBivBixBkABkVBkWBkgBklBkmBktBljBlmBlnBlxBm2Bm3BmABmVBmWBmbBmgBmlBmmBmsBnABnFBnVBnWBnjBnmBnsBoVBpABpFBpVBpWBpcBpsBsrBstBviBxiC(1)C(2)C(3)C(4)C(5)C(6)C(7)C(8)C(9)C(A)C(B)C(C)C(D)C(E)C(F)C(G)C(H)C(I)C(J)C(K)C(L)C(M)C(N)C(O)C(P)C(Q)C(R)C(S)C(T)C(U)C(V)C(W)C(X)C(Y)C(Z)C(a)C(b)C(c)C(d)C(e)C(f)C(g)C(h)C(i)C(j)C(k)C(l)C(m)C(n)C(o)C(p)C(q)C(r)C(s)C(t)C(u)C(v)C(w)C(x)C(y)C(z)C...C10.C11.C12.C13.C14.C15.C16.C17.C18.C19.C20.C::=C===CCo.CFAXCGHzCGPaCIIICLTDCL /bin/podmansh: 0,1,2,3,4,5,6,7,8,9,(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)(m)(n)(o)(p)(q)(r)(s)(t)(u)(v)(w)(x)(y)(z) /bin/podmansh: B!!B!?B..B0,B0.B1,B1.B10B11B12B13B14B15B16B17B18B19B2,B2.B20B21B22B23B24B25B26B27B28B29B3,B3.B30B31B32B33B34B35B36B37B38B39B4,B4.B40B41B42B43B44B45B46B47B48B49B5,B5.B50B6,B6.B7,B7.B8,B8.B9,B9.B==B?!B??BAUBBqBCDBDJBDZBDzBGBBGyBHPBHVBHgBHzBIIBIJBIUBIVBIXBKBBKKBKMBLJBLjBMBBMCBMDBMRBMVBMWBNJBNjBNoBPHBPRBPaBRsBSDBSMBSSBSvBTMBVIBWCBWZBWbBXIBccBcdBcmBdBBdaBdlBdmBdzBeVBffBfiBflBfmBhaBiiBijBinBivBixBkABkVBkWBkgBklBkmBktBljBlmBlnBlxBm2Bm3BmABmVBmWBmbBmgBmlBmmBmsBnABnFBnVBnWBnjBnmBnsBoVBpABpFBpVBpWBpcBpsBsrBstBviBxiC(1)C(2)C(3)C(4)C(5)C(6)C(7)C(8)C(9)C(A)C(B)C(C)C(D)C(E)C(F)C(G)C(H)C(I)C(J)C(K)C(L)C(M)C(N)C(O)C(P)C(Q)C(R)C(S)C(T)C(U)C(V)C(W)C(X)C(Y)C(Z)C(a)C(b)C(c)C(d)C(e)C(f)C(g)C(h)C(i)C(j)C(k)C(l)C(m)C(n)C(o)C(p)C(q)C(r)C(s)C(t)C(u)C(v)C(w)C(x)C(y)C(z)C...C10.C11.C12.C13.C14.C15.C16.C17.C18.C19.C20.C::=C===CCo.CFAXCGHzCGPaCIIICLTDCL /bin/qemu-ga: Copyright (c) 2003-2023 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/rhc: 0,1,2,3,4,5,6,7,8,9,(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)(m)(n)(o)(p)(q)(r)(s)(t)(u)(v)(w)(x)(y)(z) /bin/rhc: B!!B!?B..B0,B0.B1,B1.B10B11B12B13B14B15B16B17B18B19B2,B2.B20B21B22B23B24B25B26B27B28B29B3,B3.B30B31B32B33B34B35B36B37B38B39B4,B4.B40B41B42B43B44B45B46B47B48B49B5,B5.B50B6,B6.B7,B7.B8,B8.B9,B9.B==B?!B??BAUBBqBCDBDJBDZBDzBGBBGyBHPBHVBHgBHzBIIBIJBIUBIVBIXBKBBKKBKMBLJBLjBMBBMCBMDBMRBMVBMWBNJBNjBNoBPHBPRBPaBRsBSDBSMBSSBSvBTMBVIBWCBWZBWbBXIBccBcdBcmBdBBdaBdlBdmBdzBeVBffBfiBflBfmBhaBiiBijBinBivBixBkABkVBkWBkgBklBkmBktBljBlmBlnBlxBm2Bm3BmABmVBmWBmbBmgBmlBmmBmsBnABnFBnVBnWBnjBnmBnsBoVBpABpFBpVBpWBpcBpsBsrBstBviBxiC(1)C(2)C(3)C(4)C(5)C(6)C(7)C(8)C(9)C(A)C(B)C(C)C(D)C(E)C(F)C(G)C(H)C(I)C(J)C(K)C(L)C(M)C(N)C(O)C(P)C(Q)C(R)C(S)C(T)C(U)C(V)C(W)C(X)C(Y)C(Z)C(a)C(b)C(c)C(d)C(e)C(f)C(g)C(h)C(i)C(j)C(k)C(l)C(m)C(n)C(o)C(p)C(q)C(r)C(s)C(t)C(u)C(v)C(w)C(x)C(y)C(z)C...C10.C11.C12.C13.C14.C15.C16.C17.C18.C19.C20.C::=C===CCo.CFAXCGHzCGPaCIIICLTDCL /bin/screen: Copyright (c) 2018-2020 Alexander Naumov, Amadeusz Slawinski /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/sliceprint: # Copyright (c) 1996-1999 Markku Rossi /bin/soundstretch: Copyright (c) Olli Parviainen /bin/soundstretch: SoundStretch v%s - Copyright (c) Olli Parviainen /bin/ssh-copy-id: # Copyright (c) 1999-2020 Philip Hands <phil@hands.com> /bin/strace: Copyright (c) 1991-%s The strace developers <%s>. /bin/strace-log-merge: # Copyright (c) 2012-2021 The strace developers. /bin/tree: $Version: $ tree v1.8.0 (c) 1996 - 2018 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/vm-support: # Copyright (c) 2006-2022 VMware, Inc. All rights reserved. /bin/wavpack: Copyright (c) 1998 - 2020 David Bryant. All Rights Reserved. /bin/wvgain: Copyright (c) 2005 - 2020 David Bryant. All Rights Reserved. /bin/wvtag: Copyright (c) 2018 - 2020 David Bryant. All Rights Reserved. /bin/wvunpack: Copyright (c) 1998 - 2020 David Bryant. All Rights Reserved. /bin/xgettext: UNICODE_VALUE (c) >= 0 && UNICODE_VALUE (c) < 0x110000 /bin/zip: Copyright (c) 1990-2008 Info-ZIP - Type '%s "-L"' for software license. /bin/zip: Copyright (c) 1990-2008 Info-ZIP. All rights reserved. /bin/zip: bzip2 code and library copyright (c) Julian (See the bzip2 license for t> /bin/zipcloak: Copyright (c) 1990-2008 Info-ZIP - Type '%s "-L"' for software license. /bin/zipcloak: Copyright (c) 1990-2008 Info-ZIP. All rights reserved. /bin/zipnote: Copyright (c) 1990-2008 Info-ZIP - Type '%s "-L"' for software license. /bin/zipnote: Copyright (c) 1990-2008 Info-ZIP. All rights reserved. /bin/zipsplit: Copyright (c) 1990-2008 Info-ZIP - Type '%s "-L"' for software license. /bin/zipsplit: Copyright (c) 1990-2008 Info-ZIP. All rights reserved.
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@redhat9 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@redhat9 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@redhat9 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@redhat9 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@redhat9 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@redhat9 tmp]#
If the value of y is negative, head displays all bytes in the file except the last y bytes:
[root@redhat9 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@redhat9 tmp]#
Both x and y can accept multipliers:
[root@redhat9 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@redhat9 tmp]# [root@redhat9 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@redhat9 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@redhat9 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@redhat9 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@redhat9 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@redhat9 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@redhat9 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@redhat9 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@redhat9 tmp]# tail -f /var/log/messages Sep 25 15:59:09 redhat9 systemd[1]: packagekit.service: Consumed 27.056s CPU time. Sep 25 16:21:00 redhat9 systemd[1]: Started /usr/bin/systemctl start man-db-cache-update. Sep 25 16:21:00 redhat9 systemd[1]: Starting man-db-cache-update.service... Sep 25 16:21:01 redhat9 systemd[1]: Starting PackageKit Daemon... Sep 25 16:21:01 redhat9 systemd[1]: Started PackageKit Daemon. Sep 25 16:21:01 redhat9 dbus-broker[743]: A security policy denied :1.25 to send method call /org/freedesktop/PackageKit:org.freedesktop.DBus.Properties.GetAll to :1.99. Sep 25 16:21:04 redhat9 systemd[1]: man-db-cache-update.service: Deactivated successfully. Sep 25 16:21:04 redhat9 systemd[1]: Finished man-db-cache-update.service. Sep 25 16:21:04 redhat9 systemd[1]: run-r92bee130695b430689098f98ad81e47d.service: Deactivated successfully. Sep 25 16:26:07 redhat9 systemd[1]: packagekit.service: Deactivated successfully. ^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@redhat9 tmp]# ifconfig ens18 ens18: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.2.101 netmask 255.255.255.0 broadcast 10.0.2.255 inet6 fe80::9086:d7ff:fe66:e75a prefixlen 64 scopeid 0x20<link> ether 92:86:d7:66:e7:5a txqueuelen 1000 (Ethernet) RX packets 63639 bytes 160337691 (152.9 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 52302 bytes 7554310 (7.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@redhat9 tmp]# ifconfig ens18 | grep "inet" inet 10.0.2.101 netmask 255.255.255.0 broadcast 10.0.2.255 inet6 fe80::9086:d7ff:fe66:e75a prefixlen 64 scopeid 0x20<link> [root@redhat9 tmp]# ifconfig ens18 | grep "inet" | grep -v "inet6" inet 10.0.2.101 netmask 255.255.255.0 broadcast 10.0.2.255 [root@redhat9 tmp]# ifconfig ens18 | grep "inet" | grep -v "inet6" | tr -s " " ":" :inet:10.0.2.101:netmask:255.255.255.0:broadcast:10.0.2.255 [root@redhat9 tmp]# ifconfig ens18 | grep "inet" | grep -v "inet6" | tr -s " " ":" | cut -d: -f3 10.0.2.101
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@redhat9 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 92:86:d7:66:e7:5a brd ff:ff:ff:ff:ff:ff altname enp0s18 inet 10.0.2.101/24 brd 10.0.2.255 scope global noprefixroute ens18 valid_lft forever preferred_lft forever inet6 fe80::9086:d7ff:fe66:e75a/64 scope link noprefixroute valid_lft forever preferred_lft forever [root@redhat9 tmp]# ip addr show ens18 | grep "inet" inet 10.0.2.101/24 brd 10.0.2.255 scope global noprefixroute ens18 inet6 fe80::9086:d7ff:fe66:e75a/64 scope link noprefixroute [root@redhat9 tmp]# ip addr show ens18 | grep "inet" | grep -v "inet6" inet 10.0.2.101/24 brd 10.0.2.255 scope global noprefixroute ens18 [root@redhat9 tmp]# ip addr show ens18 | grep "inet" | grep -v "inet6" | awk '{ print $2; }' 10.0.2.101/24 [root@redhat9 tmp]# ip addr show ens18 | grep "inet" | grep -v "inet6" | awk '{ print $2; }' | sed 's/\/.*$//' 10.0.2.101
Copyright © 2024 Hugh Norris.