Ceci est une ancienne révision du document !
Table des matières
Last updated on: 2020/01/30 03:28
Basic Shell Commands and Text Manipulation Tools
To do - You are currently the root user in your terminal. Before proceeding further, type exit and hit the ↵ Enter key.
Use of Basic Shell Commands
The stty Command
Using this command with the -a switch allows you to identify which combination of keys should be used to control a foreground process:
[trainee@centos7 ~]$ stty -a speed 38400 baud; rows 26; columns 80; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^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
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.
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@centos7 ~]$ date Thu 29 Sep 04:53:58 CEST 2016
Command Line Switches
To do : Use the –help option of the date command to view the command line switches.
The who Command
This command's output shows who is currently connected to the system:
[trainee@centos7 ~]$ who trainee pts/0 2016-09-28 23:29 (gateway)
Command Line Switches
To do : Use the –help option of the who command to view the command line switches.
The df Command
This command's output shows the free space on each mounted block device:
[trainee@centos7 ~]$ df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 10229760 4274948 5954812 42% / devtmpfs 236036 0 236036 0% /dev tmpfs 250388 0 250388 0% /dev/shm tmpfs 250388 4716 245672 2% /run tmpfs 250388 0 250388 0% /sys/fs/cgroup tmpfs 250388 72 250316 1% /tmp /dev/sda1 201388 201368 20 100% /boot tmpfs 50080 0 50080 0% /run/user/1000
The units are shown as blocks. In order to humanize the output, it is possible to use the -h switch (also known as a parameter, option or flag):
[trainee@centos7 ~]$ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 9.8G 4.1G 5.7G 42% / devtmpfs 231M 0 231M 0% /dev tmpfs 245M 0 245M 0% /dev/shm tmpfs 245M 4.7M 240M 2% /run tmpfs 245M 0 245M 0% /sys/fs/cgroup tmpfs 245M 72K 245M 1% /tmp /dev/sda1 197M 197M 20K 100% /boot tmpfs 49M 0 49M 0% /run/user/1000
Command Line Switches
To do : Use the –help option of the df command to view the command line switches.
The free Command
This command's output shows the memory usage:
[trainee@centos7 ~]$ free total used free shared buff/cache available Mem: 500780 192460 11580 3356 296740 245128 Swap: 2096124 3852 2092272
The units are shown as blocks. In order to humanize the output, it is possible to use the -h switch:
[trainee@centos7 ~]$ free -h total used free shared buff/cache available Mem: 489M 188M 11M 3.3M 289M 239M Swap: 2.0G 3.8M 2.0G
Command Line Switches
To do : Use the –help option of the free command to view the command line switches.
The whoami Command
This command's output indicates the user name associated with the current effective user ID:
[trainee@centos7 ~]$ whoami trainee
Now become the system administrator root:
[trainee@centos7 ~]$ su - Password: fenestros Last login: Wed Sep 28 12:08:28 CEST 2016 on pts/0
Important : Note that the password will not be visible.
Now use the whoami command again:
[root@centos7 ~]# whoami root [root@centos7 ~]#
Important : Note the current effective user ID is root.
Finally execute the exit command to return as trainee:
[root@centos7 ~]# exit logout [trainee@centos7 ~]$
Command Line Switches
To do : Use the –help option of the whoami command to view the command line switches.
The pwd Command
This command's output shows the current working directory:
[trainee@centos7 ~]$ pwd /home/trainee
Command Line Switches
To do : Use the help command with pwd option to view the command line switches.
The cd Command
This command's output changes the current working directory to that specified by the argument:
[trainee@centos7 ~]$ cd /tmp [trainee@centos7 tmp]$ pwd /tmp [trainee@centos7 tmp]$
Command Line Switches
To do : Use the help command with cd option to view the command line switches.
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@centos7 tmp]$ ls hsperfdata_root inode systemd-private-45071248a58f4fd2a4de940a5734c8ec-cups.service-ODc5L6
Command Line Switches
To do : Use the –help option of the ls command to view the command line switches.
The lsof Command
This command's output shows information about open files:
[trainee@centos7 tmp]$ su - Password: fenestros Last login: Thu Sep 29 06:24:28 CEST 2016 on pts/0 [root@centos7 ~]# lsof | more COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME systemd 1 root cwd DIR 8,2 4096 128 / systemd 1 root rtd DIR 8,2 4096 128 / systemd 1 root txt REG 8,2 1494056 33607555 /usr/lib/systemd/systemd systemd 1 root mem REG 8,2 20032 17241167 /usr/lib64/libuuid.so.1.3.0 systemd 1 root mem REG 8,2 252696 20114965 /usr/lib64/libblkid.so.1.1.0 systemd 1 root mem REG 8,2 90632 18751025 /usr/lib64/libz.so.1.2.7 systemd 1 root mem REG 8,2 19888 17326380 /usr/lib64/libattr.so.1.1.0 systemd 1 root mem REG 8,2 19520 17239651 /usr/lib64/libdl-2.17.so systemd 1 root mem REG 8,2 153192 18023397 /usr/lib64/liblzma.so.5.0.99 systemd 1 root mem REG 8,2 398272 18105157 /usr/lib64/libpcre.so.1.2.0 systemd 1 root mem REG 8,2 2107816 17239525 /usr/lib64/libc-2.17.so systemd 1 root mem REG 8,2 142304 17240882 /usr/lib64/libpthread-2.17.so systemd 1 root mem REG 8,2 88720 17511831 /usr/lib64/libgcc_s-4.8.5-20150702.so.1 systemd 1 root mem REG 8,2 44096 17241011 /usr/lib64/librt-2.17.so systemd 1 root mem REG 8,2 260784 20114966 /usr/lib64/libmount.so.1.1.0 systemd 1 root mem REG 8,2 91768 18023376 /usr/lib64/libkmod.so.2.2.10 systemd 1 root mem REG 8,2 118792 17326381 /usr/lib64/libaudit.so.1.0.0 systemd 1 root mem REG 8,2 61648 18105139 /usr/lib64/libpam.so.0.83.1 systemd 1 root mem REG 8,2 20024 17326423 /usr/lib64/libcap.so.2.22 systemd 1 root mem REG 8,2 147120 18447456 /usr/lib64/libselinux.so.1 systemd 1 root mem REG 8,2 164440 21744303 /usr/lib64/ld-2.17.so systemd 1 root mem REG 8,2 1333123 50334524 /etc/selinux/targeted/contexts/files/file_contexts.bin --More--
Command Line Switches
To do : Use the –help option of the lsof command to view the command line switches.
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@centos7 ~]# exit logout [trainee@centos7 tmp]$ touch test [trainee@centos7 tmp]$ ls hsperfdata_root systemd-private-45071248a58f4fd2a4de940a5734c8ec-cups.service-ODc5L6 inode test
Command Line Switches
To do : Use the –help option of the touch command to view the command line switches.
The echo Command
This command writes the arguments to the standard output (i.e. the screen):
[trainee@centos7 tmp]$ echo fenestros fenestros
Command Line Switches
To do : Use the help command with echo option to view the command line switches.
The cp Command
This command is used to copy a source to a destination or multiple sources to a directory:
[trainee@centos7 tmp]$ cp test ~ [trainee@centos7 tmp]$ ls -l ~ total 4 drwxr-xr-x. 2 trainee trainee 6 Apr 30 11:54 Desktop drwxr-xr-x. 2 trainee trainee 6 Apr 30 11:54 Documents drwxr-xr-x. 2 trainee trainee 6 Apr 30 11:54 Downloads drwxr-xr-x. 2 trainee trainee 6 Apr 30 11:54 Music drwxr-xr-x. 2 trainee trainee 6 Apr 30 11:54 Pictures drwxr-xr-x. 2 trainee trainee 6 Apr 30 11:54 Public drwxr-xr-x. 2 trainee trainee 6 Apr 30 11:54 Templates -rw-rw-r--. 1 trainee trainee 0 Sep 29 17:23 test drwxr-xr-x. 2 trainee trainee 6 Apr 30 11:54 Videos -rw-rw-r--. 1 trainee trainee 442 Sep 29 00:53 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.
The file Command
This command determines a file type:
[trainee@centos7 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@centos7 tmp]$ echo "fenestros" > ~/test
Now use the file command once again to determine the file type:
[trainee@centos7 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.
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@centos7 tmp]$ cat ~/test fenestros
Command Line Switches
To do : Use the –help option of the cat command to view the command line switches.
The mv Command
This command renames a source to a destination or moves sources to a directory:
[trainee@centos7 tmp]$ mv ~/test . [trainee@centos7 tmp]$ ls -l ~ total 4 drwxr-xr-x. 2 trainee trainee 6 Apr 30 11:54 Desktop drwxr-xr-x. 2 trainee trainee 6 Apr 30 11:54 Documents drwxr-xr-x. 2 trainee trainee 6 Apr 30 11:54 Downloads drwxr-xr-x. 2 trainee trainee 6 Apr 30 11:54 Music drwxr-xr-x. 2 trainee trainee 6 Apr 30 11:54 Pictures drwxr-xr-x. 2 trainee trainee 6 Apr 30 11:54 Public drwxr-xr-x. 2 trainee trainee 6 Apr 30 11:54 Templates drwxr-xr-x. 2 trainee trainee 6 Apr 30 11:54 Videos -rw-rw-r--. 1 trainee trainee 442 Sep 29 00:53 vitext [trainee@centos7 tmp]$ mv test TeSt [trainee@centos7 tmp]$ ls -l total 4 drwxr-xr-x. 2 root root 80 Sep 28 10:52 hsperfdata_root drwxr-xr-x. 2 root root 100 Sep 28 12:30 inode drwx------. 3 root root 60 Sep 28 10:49 systemd-private-45071248a58f4fd2a4de940a5734c8ec-cups.service-ODc5L6 -rw-rw-r--. 1 trainee trainee 10 Sep 29 17:28 TeSt
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.
The mkdir Command
This command creates the directory(ies) if it (they) does (do) not exist:
[trainee@centos7 tmp]$ cd ~ [trainee@centos7 ~]$ mkdir testdir [trainee@centos7 ~]$ 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.
The rmdir Command
This command removes the directory(ies) if it (they) is (are) empty:
[trainee@centos7 ~]$ rmdir testdir [trainee@centos7 ~]$ 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.
The rm Command
This command removes a directory, empty of not, as well as files:
[trainee@centos7 ~]$ mkdir testdir1 [trainee@centos7 ~]$ cd /tmp [trainee@centos7 tmp]$ echo "fenestros" > TeSt [trainee@centos7 tmp]$ cd ~ [trainee@centos7 ~]$ mv /tmp/TeSt ~/testdir1 [trainee@centos7 ~]$ ls -lR testdir1/ testdir1/: total 4 -rw-rw-r--. 1 trainee trainee 10 Sep 29 18:13 TeSt [trainee@centos7 ~]$ rmdir testdir1/ rmdir: failed to remove ‘testdir1/’: Directory not empty [trainee@centos7 ~]$ rm -rf testdir1/ [trainee@centos7 ~]$ 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.
The sort Command
This command writes a sorted concatenation of all files to standard output:
[trainee@centos7 ~]$ touch aac abc bca xyz [trainee@centos7 ~]$ ls aac bca Documents Music Public Videos xyz abc Desktop Downloads Pictures Templates vitext [trainee@centos7 ~]$ ls | sort aac abc bca Desktop Documents Downloads Music Pictures Public Templates Videos vitext xyz [trainee@centos7 ~]$ 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.
The more Command
This command is used to display a long file page by page:
[trainee@centos7 ~]$ more /etc/services # /etc/services: # $Id: services,v 1.55 2013/04/14 ovasik Exp $ # # Network services, Internet style # IANA services version: last updated 2013-04-10 # # Note that it is presently the policy of IANA to assign a single well-known # port number for both TCP and UDP; hence, most entries here have two entries # even if the protocol doesn't support UDP operations. # Updated from RFC 1700, ``Assigned Numbers'' (October 1994). Not all ports # are included, only the more common ones. # # The latest IANA port assignments can be gotten from # http://www.iana.org/assignments/port-numbers # The Well Known Ports are those from 0 through 1023. # The Registered Ports are those from 1024 through 49151 # The Dynamic and/or Private Ports are those from 49152 through 65535 # # Each line describes one service, and is of the form: # # service-name port/protocol [aliases ...] [# comment] tcpmux 1/tcp # TCP port service multiplexer --More--(0%)
Important - The ↵ Enter key scrolls down line by line, the Space Bar key scrolls down page by page and the Q key comes back to the command line prompt.
Command Line Switches
To do : Use the –help option of the more command to view the command line switches.
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@centos7 ~]$ less /etc/services # /etc/services: # $Id: services,v 1.55 2013/04/14 ovasik Exp $ # # Network services, Internet style # IANA services version: last updated 2013-04-10 # # Note that it is presently the policy of IANA to assign a single well-known # port number for both TCP and UDP; hence, most entries here have two entries # even if the protocol doesn't support UDP operations. # Updated from RFC 1700, ``Assigned Numbers'' (October 1994). Not all ports # are included, only the more common ones. # # The latest IANA port assignments can be gotten from # http://www.iana.org/assignments/port-numbers # The Well Known Ports are those from 0 through 1023. # The Registered Ports are those from 1024 through 49151 # The Dynamic and/or Private Ports are those from 49152 through 65535 # # Each line describes one service, and is of the form: # # service-name port/protocol [aliases ...] [# comment] tcpmux 1/tcp # TCP port service multiplexer /etc/services
Command Line Switches
To do : Use the –help option of the less command to view the command line switches.
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@centos7 ~]$ find acc find: ‘acc’: No such file or directory [trainee@centos7 ~]$ 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.
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@centos7 ~]$ su - Password: fenestros Last login: Thu Sep 29 17:08:56 CEST 2016 on pts/0
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.
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@centos7 ~]# ls -l /var/lib/mlocate/mlocate.db -rw-r-----. 1 root slocate 3887117 Sep 29 03:46 /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@centos7 ~]# 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 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" PRUNENAMES = ".git .hg .svn" PRUNEPATHS = "/afs /media /mnt /net /sfs /tmp /udev /var/cache/ccache /var/lib/yum/yumdb /var/spool/cups /var/spool/squid /var/tmp"
Use of these two commands is very simple:
[root@centos7 ~]# updatedb [root@centos7 ~]# locate aac /home/trainee/aac /usr/lib/modules/3.10.0-327.13.1.el7.x86_64/kernel/drivers/scsi/aacraid /usr/lib/modules/3.10.0-327.13.1.el7.x86_64/kernel/drivers/scsi/aacraid/aacraid.ko /usr/lib/modules/3.10.0-327.el7.x86_64/kernel/drivers/scsi/aacraid /usr/lib/modules/3.10.0-327.el7.x86_64/kernel/drivers/scsi/aacraid/aacraid.ko /usr/lib/python2.7/site-packages/ipalib/plugins/caacl.py /usr/lib/python2.7/site-packages/ipalib/plugins/caacl.pyc /usr/lib/python2.7/site-packages/ipalib/plugins/caacl.pyo /usr/share/gtk-doc/html/gst-plugins-good-plugins-1.0/gst-plugins-good-plugins-aacparse.html /usr/share/gupnp-dlna-2.0/dlna-profiles/aac.xml /usr/share/mime/audio/aac.xml /usr/src/kernels/3.10.0-327.13.1.el7.x86_64/drivers/scsi/aacraid /usr/src/kernels/3.10.0-327.13.1.el7.x86_64/drivers/scsi/aacraid/Makefile /usr/src/kernels/3.10.0-327.13.1.el7.x86_64/include/config/scsi/aacraid.h /usr/src/kernels/3.10.0-327.el7.x86_64/drivers/scsi/aacraid /usr/src/kernels/3.10.0-327.el7.x86_64/drivers/scsi/aacraid/Makefile /usr/src/kernels/3.10.0-327.el7.x86_64/include/config/scsi/aacraid.h
Command Line Switches
To do : Use the –help option of the updatedb and locate commands to view their command line switches.
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@centos7 ~]# whereis passwd passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz
Command Line Switches
To do : Use the –help option of the whereis command to view the command line switches.
The which Command
This command searches the PATH variable and returns to standard output the first full path associated with the argument:
[root@centos7 ~]# which passwd /bin/passwd
Command Line Switches
To do : Use the –help option of the which command to view the command line switches.
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@centos7 ~]# uptime 19:18:57 up 1 day, 8:30, 1 user, load average: 0.10, 0.09, 0.07
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.
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@centos7 ~]# w 19:28:55 up 1 day, 8:40, 1 user, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT trainee pts/0 gateway 15:22 7.00s 0.16s 0.12s 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.
The uname Command
This command prints system information to the standard output:
[root@centos7 ~]# uname -a Linux centos7.fenestros.loc 3.10.0-327.13.1.el7.x86_64 #1 SMP Thu Mar 31 16:04:38 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux [root@centos7 ~]# uname -s Linux [root@centos7 ~]# uname -n centos7.fenestros.loc [root@centos7 ~]# uname -r 3.10.0-327.13.1.el7.x86_64 [root@centos7 ~]# uname -v #1 SMP Thu Mar 31 16:04:38 UTC 2016 [root@centos7 ~]# uname -m x86_64 [root@centos7 ~]# uname -p x86_64 [root@centos7 ~]# uname -i x86_64 [root@centos7 ~]# uname -o GNU/Linux
Command Line Switches
To do : Use the –help option of the uname command to view the command line switches.
The du Command
This command summarizes disk usage of each file, recursively for directories:
[root@centos7 ~]# du -sh /* 2>/dev/null 0 /bin 187M /boot 0 /dev 33M /etc 20M /home 0 /lib 0 /lib64 0 /lost+found 0 /media 0 /mnt 173M /opt 0 /proc 43M /root 8.7M /run 0 /sbin 0 /srv 0 /sys 64K /tmp 3.6G /usr 228M /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.
The clear Command
This command is used to clear the current screen of the terminal:
[root@centos7 ~]# clear [root@centos7 ~]#
The exit Command
This command exits the current shell:
[root@centos7 ~]# exit logou [trainee@centos7 ~]$
Command Line Switches
To do : Use the help command with exit option to view the command line switches.
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.
The sleep Command
This command pauses for a number seconds. The number is specified as the first argument.
Command Line Switches
To do : Use the –help option of the sleep command to view the command line switches.
The wall Command
Wall sends a message to everybody logged in with their mesg(1) permission set to yes. The message can be given as an argument to wall, or it can be sent to wall's standard input. When using the standard input from a terminal, the message should be terminated with the EOF key (usually Control-D). The length of the message is limited to 20 lines. For every invocation of wall a notification will be written to syslog, with facility LOG_USER and level LOG_INFO.
Start a second session as trainee via ssh on your VM. Return to your first session as root and type :
[root@centos7 ~]# wall this is a message from root [root@centos7 ~]# Broadcast message from root@centos7.fenestros.loc (pts/0) (Fri Sep 30 05:35:26 2016): this is a message from root [Enter] [root@centos7 ~]#
In the second session you should see the following message :
[trainee@centos7 ~]$ Broadcast message from root@centos7.fenestros.loc (pts/0) (Fri Sep 30 05:35:26 2016): this is a message from root
Now look at the end of the/var/log/messages file :
[root@centos7 ~]# tail /var/log/messages Sep 30 05:31:08 centos7 sh: Sleeping '' '' Sep 30 05:34:16 centos7 sh: Sleeping '' '' Sep 30 05:35:13 centos7 systemd: Started Session 311 of user trainee. Sep 30 05:35:13 centos7 systemd-logind: New session 311 of user trainee. Sep 30 05:35:13 centos7 systemd: Starting Session 311 of user trainee. Sep 30 05:35:13 centos7 dbus[515]: [system] Activating service name='org.freedesktop.problems' (using servicehelper) Sep 30 05:35:13 centos7 dbus-daemon: dbus[515]: [system] Activating service name='org.freedesktop.problems' (using servicehelper) Sep 30 05:35:13 centos7 dbus[515]: [system] Successfully activated service 'org.freedesktop.problems' Sep 30 05:35:13 centos7 dbus-daemon: dbus[515]: [system] Successfully activated service 'org.freedesktop.problems' Sep 30 05:35:26 centos7 wall[27550]: wall: user root broadcasted 1 lines (29 chars)
Important : La commande wall ignore la variable d’environnement TZ. L’heure affichée dans la première page est basée sur les paramètres de régionalisation du système.
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@centos7 ~]# seq 10 1 2 3 4 5 6 7 8 9 10 [root@centos7 ~]# seq 20 30 20 21 22 23 24 25 26 27 28 29 30 [root@centos7 ~]# seq 20 10 90 20 30 40 50 60 70 80 90 [root@centos7 ~]#
Command Line Switches
To do : Use the –help option of the seq command to view the command line switches.
The screen Command
Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells). Each virtual terminal provides the functions of a DEC VT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI X3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows moving text regions between windows.
The screen command is not installed by default under RHEL/CentOS 7:
[root@centos7 ~]# which screen /usr/bin/which: no screen in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin) [root@centos7 ~]# yum install screen Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: ftp.rezopole.net * extras: centos.mirror.ate.info * updates: centos.mirror.fr.planethoster.net Resolving Dependencies --> Running transaction check ---> Package screen.x86_64 0:4.1.0-0.23.20120314git3c2946.el7_2 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================= Package Arch Version Repository Size ================================================================================= Installing: screen x86_64 4.1.0-0.23.20120314git3c2946.el7_2 updates 552 k Transaction Summary ================================================================================= Install 1 Package Total download size: 552 k Installed size: 914 k Is this ok [y/d/N]: y Downloading packages: screen-4.1.0-0.23.20120314git3c2946.el7_2.x86_64.rpm | 552 kB 00:00:01 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : screen-4.1.0-0.23.20120314git3c2946.el7_2.x86_64 1/1 Verifying : screen-4.1.0-0.23.20120314git3c2946.el7_2.x86_64 1/1 Installed: screen.x86_64 0:4.1.0-0.23.20120314git3c2946.el7_2 Complete! [root@centos7 ~]# which screen /bin/screen
Create a session with screen:
[root@centos7 ~]# screen -S mysession
Important - When using RHEL/CentOS 7, you will notice that the title of the window holding the ssh session changes to [screen 0: root@centos7:~].
Now press the CTRL and A keys, release the A key and press the C key in order to create a second nested screen.
Important - When using RHEL/CentOS 7, you will notice that the title of the window holding the ssh session changes to [screen 1: root@centos7:~].
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@centos7 ~]# screen -ls There is a screen on: 25812.mysession (Attached) 1 Socket in /var/run/screen/S-root.
Now enter the following commands:
[root@centos7 ~]# sleep 9999 & [1] 2272 [root@centos7 ~]# jobs [1]+ Running sleep 9999 & [root@centos7 ~]#
In order to detach the current screen press the CTRL and A keys, release the A key and press the D key:
[root@centos7 ~]# screen -S mysession [detached from 25812.mysession] [root@centos7 ~]#
To re-attach the screen, execute the following command:
[root@centos7 ~]# screen -r
Using the jobs command, check if the process created by the sleep command is still running:unit
[root@centos7 ~]# jobs [1]+ Running sleep 9999 &
To move forward or backwards between screens press the CTRL and A keys, release the A key and press the N key or press the CTRL and A keys, release the A key and press the P key.
Once again detach the current screen by pressing the CTRL A keys, releasing the A key and then pressing D:
[root@centos7 ~]# screen -S mysession [detached from 25812.mysession] [root@centos7 ~]#
Now create a new, non-nested screen:
[root@centos7 ~]# screen -S mysession1
Use the screen -ls command to see what has happened:
[root@centos7 ~]# screen -ls There are screens on: 14942.mysession1 (Attached) 25812.mysession (Detached) 2 Sockets in /var/run/screen/S-root.
To re-attach a specific screen, reference it by it's number:
[root@centos7 ~]# screen -r 25812
Finally, check out what has happened:
[root@centos7 ~]# sleep 9999 & [1] 2272 [root@centos7 ~]# jobs [1]+ Running sleep 9999 & [root@centos7 ~]# jobs [1]+ Running sleep 9999 & [root@centos7 ~]# screen -ls There is a screen on: 25812.mysession (Attached) 1 Socket in /var/run/screen/S-root. [root@centos7 ~]# screen -ls There are screens on: 14942.mysession1 (Attached) 25812.mysession (Attached) 2 Sockets in /var/run/screen/S-root.
Command Line Switches
To do : Use the –help option of the screen command to view the command line switches.
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@centos7 ~]# ls -lai /tmp total 244 11071 drwxrwxrwt. 10 root root 260 Sep 30 06:24 . 128 dr-xr-xr-x. 18 root root 4096 Dec 10 2015 .. 13635 drwxrwxrwt. 2 root root 40 Sep 28 10:48 .font-unix 24858 drwxr-xr-x. 2 root root 100 Sep 30 06:05 hsperfdata_root 13597 drwxrwxrwt. 2 root root 40 Sep 28 10:48 .ICE-unix 287056 drwxr-xr-x. 2 root root 100 Sep 28 12:30 inode 24861 srw-------. 1 root root 0 Sep 28 10:52 .java_pid3213 25344 srw-------. 1 root root 0 Sep 28 10:52 .java_pid3302 16615 drwx------. 3 root root 60 Sep 28 10:49 systemd-private-45071248a58f4fd2a4de940a5734c8ec-cups.service-ODc5L6 13595 drwxrwxrwt. 2 root root 40 Sep 28 10:48 .Test-unix 13590 drwxrwxrwt. 2 root root 40 Sep 28 10:48 .X11-unix 13629 drwxrwxrwt. 2 root root 40 Sep 28 10:48 .XIM-unix 8098838 -rw-------. 1 root root 242724 Sep 30 05:45 yum_save_tx.2016-09-30.05-45.FF7KO4.yumtx [root@centos7 ~]# ls -ali /tmp total 244 11071 drwxrwxrwt. 10 root root 260 Sep 30 06:24 . 128 dr-xr-xr-x. 18 root root 4096 Dec 10 2015 .. 13635 drwxrwxrwt. 2 root root 40 Sep 28 10:48 .font-unix 24858 drwxr-xr-x. 2 root root 100 Sep 30 06:05 hsperfdata_root 13597 drwxrwxrwt. 2 root root 40 Sep 28 10:48 .ICE-unix 287056 drwxr-xr-x. 2 root root 100 Sep 28 12:30 inode 24861 srw-------. 1 root root 0 Sep 28 10:52 .java_pid3213 25344 srw-------. 1 root root 0 Sep 28 10:52 .java_pid3302 16615 drwx------. 3 root root 60 Sep 28 10:49 systemd-private-45071248a58f4fd2a4de940a5734c8ec-cups.service-ODc5L6 13595 drwxrwxrwt. 2 root root 40 Sep 28 10:48 .Test-unix 13590 drwxrwxrwt. 2 root root 40 Sep 28 10:48 .X11-unix 13629 drwxrwxrwt. 2 root root 40 Sep 28 10:48 .XIM-unix 8098838 -rw-------. 1 root root 242724 Sep 30 05:45 yum_save_tx.2016-09-30.05-45.FF7KO4.yumtx
However ls -l –all –inode cannot be written ls -l –allinode:
[root@centos7 ~]# ls -l --all --inode /tmp total 244 11071 drwxrwxrwt. 10 root root 260 Sep 30 06:24 . 128 dr-xr-xr-x. 18 root root 4096 Dec 10 2015 .. 13635 drwxrwxrwt. 2 root root 40 Sep 28 10:48 .font-unix 24858 drwxr-xr-x. 2 root root 100 Sep 30 06:05 hsperfdata_root 13597 drwxrwxrwt. 2 root root 40 Sep 28 10:48 .ICE-unix 287056 drwxr-xr-x. 2 root root 100 Sep 28 12:30 inode 24861 srw-------. 1 root root 0 Sep 28 10:52 .java_pid3213 25344 srw-------. 1 root root 0 Sep 28 10:52 .java_pid3302 16615 drwx------. 3 root root 60 Sep 28 10:49 systemd-private-45071248a58f4fd2a4de940a5734c8ec-cups.service-ODc5L6 13595 drwxrwxrwt. 2 root root 40 Sep 28 10:48 .Test-unix 13590 drwxrwxrwt. 2 root root 40 Sep 28 10:48 .X11-unix 13629 drwxrwxrwt. 2 root root 40 Sep 28 10:48 .XIM-unix 8098838 -rw-------. 1 root root 242724 Sep 30 05:45 yum_save_tx.2016-09-30.05-45.FF7KO4.yumtx [root@centos7 ~]# 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.
Manipulating Text Files
Text files play a very important role under Linux. For example, almost all configuration files are simple text files and being able to manipulate them is of great importance. Manipulating text files is essentially achieved by using Regular Expressions. There are two types of Regular Expressions:
- The IEEE POSIX Basic Regular Expressions (BRE) understood by the commands vi, grep, expr and sed,
- The IEEE POSIX Extended Regular Expressions (ERE) understood by the commands egrep ( grep -E ) and awk.
Regular Expressions
Regular Expressions use Metacharacters. Certain are common to both BREs and EREs:
Metacharacter | Description |
---|---|
^string | Match lines beginning with string |
string$ | Match lines ending with string |
\Metacharacter | Cancel any special effect associated with Metacharacter |
[string] | Match any of the characters within string |
[^string] | Exclude any of the characters in string |
. | Match any character except when at the end of a line |
character* | Match 0 or more occurrences of character |
\< | Match string at the beginning of a word |
\> | Match string at the end of a word |
BREs
Certain Metacharacters are specific to BREs:
Metacharacter | Description |
---|---|
\{x,y\} | Match from x to y occurrences of the preceeding element |
\{x\} | Match exactly x occurrences of the preceeding element |
\{x,\} | Match x or more occurrences of the preceeding element |
\(BRE) | Commit to memory the BRE |
\1 | Recall the first BRE committed to memory |
\2, \3 … | Recall the second BRE committed to memory, recall the third BRE committed to memory … |
EREs
Certain Metacharacters are specific to EREs:
Metacharacter | Description |
---|---|
{x,y} | Match from x to y occurrences of the preceeding element |
{x} | Match exactly x occurrences of the preceeding element |
{x,} | |
? | Matches 0 or 1 occurrence of the preceeding element |
+ | Matches 1 or more occurrence(s) of the preceeding element |
| | Matches either the expression before or the expression after the operator |
() | Combines the Reguler Expressions between the parentheses |
Text-search Utilities
The grep Command
The grep command can be used to find lines containing a string of characters in a group of files. The -v or –invert-case option can be stipulated to find lines that do not contain the specified string.
The grep command is case sensitive. The -i or –ignore-case option can be specified in order to use grep in a non case sensitive search.
The grep command can also use BREs.
Command Line Switches
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 #1 - Using grep, egrep and fgrep
Download the following file by clicking on it's title:
- greptest
fenestrOS fenestros 555-5555 f .fenestros .fe £
Move the file to the /tmp directory:
[root@centos7 ~]# mv /home/trainee/Downloads/greptest /tmp/greptest
Now use grep to search for lines containing at least one uppercase or lowercase letter:
[root@centos7 ~]# 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@centos7 ~]# 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@centos7 ~]# 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@centos7 ~]# grep '^.$' /tmp/greptest f £
To search for a line containing a special character such as ., that character needs to be preceded by \:
[root@centos7 ~]# grep '^\.' /tmp/greptest .fenestros .fe
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”.
Download the following file by clicking on it's title:
- greptest
# Starting comment fenestrOS fenestros # Another comment 555-5555 f .fenestros .fe £ # End comment
Move the file to the /tmp directory:
[root@centos7 ~]# mv /home/trainee/Downloads/greptest /tmp/greptest
Now use the grep command with the -E switch to remove all the comments and empty lines:
[root@centos7 ~]# grep -E -v '^(#|$)' /tmp/greptest fenestrOS fenestros 555-5555 f .fenestros .fe £
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@centos7 ~]# egrep -v '^(#|$)' /tmp/greptest > /tmp/greptest1 [root@centos7 ~]# 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.
Download the following file by clicking on it's title:
- 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
Move the file to the /tmp directory:
[root@centos7 ~]# mv /home/trainee/Downloads/greptest /tmp/greptest
Now use fgrep to match the line starting with the ^ character:
[root@centos7 ~]# 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@centos7 ~]# 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@centos7 ~]# 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 #2 - Using sed
Start by displaying the contents of the file /etc/services whilst inhibiting the display of the first 10 lines:
[root@centos7 ~]# 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@centos7 ~]# 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@centos7 ~]# 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@centos7 ~]# 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@centos7 ~]# sed -n '/^#/!w /tmp/sedtest' /etc/services [root@centos7 ~]# 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@centos7 ~]# echo "user1,user2,user3" > /tmp/sedtest1 [root@centos7 ~]# 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@centos7 ~]# ls -l | awk '{print $8 $3 $4}' 2015rootroot 19:09270users 2015rootroot 2015rootroot
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@centos7 ~]# ls -l | awk '{print $8 " " $3 " " $4}' 2015 root root 19:09 270 users 2015 root root 2015 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@centos7 ~]# cat > awkscript BEGIN { print "List of the currently mounted file systems"} {print $0} END { print "=========================================="} [^D]
Now apply the awk script to /etc/fstab :
[root@centos7 tmp]# awk -f awkscript /etc/fstab Liste des systèmes de fichiers montés # # /etc/fstab # Created by anaconda on Sun Mar 8 12:38:10 2015 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=b35de665-5ec8-4226-a533-58a1b567ac91 / xfs defaults 1 1 UUID=e8d3bd48-1386-411c-9675-41c3f8f1a309 /boot xfs defaults 1 2 UUID=11a4d11d-81e4-46a7-82e0-7796cd597dc9 swap swap defaults 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 #3 - Using awk
Download the file sales.txt by clicking on the title below:
- sales.txt
# FenestrOs.com # Annual sales by French department # 83 Desktops§100 Portables§50 Servers§21 Ipads§4 # 06 Desktops§99 Portables§60 Servers§8 Ipads§16 # 13 Desktops§130 Portables§65 Servers§12 Ipads§56
Now download the awk script sales.awk by clicking on the title below:
- 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@centos7 ~]# awk -f /home/trainee/Downloads/sales.awk /home/trainee/Downloads/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
Other Useful Commands
The expand Command
The expand command converts tabulations in a file to spaces and prints the results to STDOUT. With no file as an argument or with the - character as an argument, the command takes it's input from STDIN.
Download the following file:
- expand.txt
un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq
Move the file to the /root folder:
[root@centos7 ~]# mv /home/trainee/Downloads/expand.txt /root/expand
Use the cat command to view the contents of the file:
[root@centos7 ~]# 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@centos7 ~]# cat -vet expand un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$
Important : As you can see the tabulations are shown as ^I and the end o each line as a $.
Now use the expand command to convert the tabulations into spaces and send the result to the expand1 file:
[root@centos7 ~]# expand expand > expand1
View the resulting expand1 file with the cat command and the -vet switches:
[root@centos7 ~]# 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.
La Commande unexpand
The expand command converts spaces in a file to tabulations and prints the results to STDOUT. With no file as an argument or with the - character as an argument, the command takes it's input from STDIN.
Now use the expand command to convert the spaces in the expand1 file into tabulations and send the result to the expand2 file:
[root@centos7 ~]# 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@centos7 ~]# unexpand -a expand1 > expand2 [root@centos7 ~]# 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.
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@centos7 ~]# cut -c1-7 /etc/passwd root:x: bin:x:1 daemon: adm:x:3 lp:x:4: sync:x: shutdow halt:x: mail:x: operato games:x ftp:x:1 nobody: dbus:x: polkitd unbound colord: usbmuxd avahi:x avahi-a saslaut qemu:x: libstor rpc:x:3 rpcuser nfsnobo rtkit:x radvd:x ntp:x:3 chrony: abrt:x: pulse:x gdm:x:4 gnome-i postfix sshd:x: tcpdump trainee vboxadd tss:x:5
In order to select columns 1 to 5, columns 10 to 15 and columns 30 and higher, us the following command:
[root@centos7 ~]# 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 nobod99:99:/nologin dbus::81:Syus:/:/sbin/nologin polki:999:9lkitd:/:/sbin/nologin unbou:998:9 resolver:/etc/unbound:/sbin/nologin color997:99ord:/var/lib/colord:/sbin/nologin usbmu:113:1r:/:/sbin/nologin avahi0:70:ASD Stack:/var/run/avahi-daemon:/sbin/nologin avahioipd:x IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin saslax:996:user":/run/saslauthd:/sbin/nologin qemu:7:107:bin/nologin libstemgmt:on account for libstoragemgmt:/var/run/lsm:/sbin/nologin rpc:x32:Rpcar/lib/rpcbind:/sbin/nologin rpcus:29:29ser:/var/lib/nfs:/sbin/nologin nfsno:x:655mous NFS User:/var/lib/nfs:/sbin/nologin rtkit72:172proc:/sbin/nologin radvd5:75:rbin/nologin ntp:x38::/eologin chron994:99rony:/sbin/nologin abrt:3:173:in/nologin pulse71:171stem Daemon:/var/run/pulse:/sbin/nologin gdm:x42::/vin/nologin gnometial-s::/run/gnome-initial-setup/:/sbin/nologin postf:89:89ostfix:/sbin/nologin sshd::74:Prted SSH:/var/empty/sshd:/sbin/nologin tcpdu:72:72gin train:1000:home/trainee:/bin/bash vboxa:992:1xadd:/bin/false tss:x59:Acche trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
In order to select the 2nd, 4th and 6th column, use the following command:
[root@centos7 ~]# 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:99:/ x:81:/ x:998:/ x:997:/etc/unbound x:996:/var/lib/colord x:113:/ x:70:/var/run/avahi-daemon x:170:/var/lib/avahi-autoipd x:76:/run/saslauthd x:107:/ x:994:/var/run/lsm x:32:/var/lib/rpcbind x:29:/var/lib/nfs x:65534:/var/lib/nfs x:172:/proc x:75:/ x:38:/etc/ntp x:993:/var/lib/chrony x:173:/etc/abrt x:171:/var/run/pulse x:42:/var/lib/gdm x:991:/run/gnome-initial-setup/ x:89:/var/spool/postfix x:74:/var/empty/sshd x:72:/ x:1000:/home/trainee x:1:/var/run/vboxadd x:59:/dev/null
Important: Note the use of the -d switch to change the default seperator.
Command Line Switches
To do : Use the –help option of the cut command to view the command line switches.
The uniq Command
The following command is used to extract the Primary Group GIDs from the /etc/passwd file:
[root@centos7 ~]# cut -d: -f4 /etc/passwd | sort -n | uniq 0 1 2 4 7 12 29 32 38 42 50 59 70 72 74 75 76 81 89 99 100 107 113 170 171 172 173 991 993 994 996 997 998 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.
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@centos7 ~]# 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:99:99:NOBODY:/:/SBIN/NOLOGIN DBUS:X:81:81:SYSTEM MESSAGE BUS:/:/SBIN/NOLOGIN POLKITD:X:999:998:USER FOR POLKITD:/:/SBIN/NOLOGIN UNBOUND:X:998:997:UNBOUND DNS RESOLVER:/ETC/UNBOUND:/SBIN/NOLOGIN COLORD:X:997:996:USER FOR COLORD:/VAR/LIB/COLORD:/SBIN/NOLOGIN USBMUXD:X:113:113:USBMUXD USER:/:/SBIN/NOLOGIN AVAHI:X:70:70:AVAHI MDNS/DNS-SD STACK:/VAR/RUN/AVAHI-DAEMON:/SBIN/NOLOGIN AVAHI-AUTOIPD:X:170:170:AVAHI IPV4LL STACK:/VAR/LIB/AVAHI-AUTOIPD:/SBIN/NOLOGIN SASLAUTH:X:996:76:"SASLAUTHD USER":/RUN/SASLAUTHD:/SBIN/NOLOGIN QEMU:X:107:107:QEMU USER:/:/SBIN/NOLOGIN LIBSTORAGEMGMT:X:995:994:DAEMON ACCOUNT FOR LIBSTORAGEMGMT:/VAR/RUN/LSM:/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 NFSNOBODY:X:65534:65534:ANONYMOUS NFS USER:/VAR/LIB/NFS:/SBIN/NOLOGIN RTKIT:X:172:172:REALTIMEKIT:/PROC:/SBIN/NOLOGIN RADVD:X:75:75:RADVD USER:/:/SBIN/NOLOGIN NTP:X:38:38::/ETC/NTP:/SBIN/NOLOGIN CHRONY:X:994:993::/VAR/LIB/CHRONY:/SBIN/NOLOGIN ABRT:X:173:173::/ETC/ABRT:/SBIN/NOLOGIN PULSE:X:171:171:PULSEAUDIO SYSTEM DAEMON:/VAR/RUN/PULSE:/SBIN/NOLOGIN GDM:X:42:42::/VAR/LIB/GDM:/SBIN/NOLOGIN GNOME-INITIAL-SETUP:X:993:991::/RUN/GNOME-INITIAL-SETUP/:/SBIN/NOLOGIN POSTFIX:X:89:89::/VAR/SPOOL/POSTFIX:/SBIN/NOLOGIN SSHD:X:74:74:PRIVILEGE-SEPARATED SSH:/VAR/EMPTY/SSHD:/SBIN/NOLOGIN TCPDUMP:X:72:72::/:/SBIN/NOLOGIN TRAINEE:X:1000:1000:TRAINEE:/HOME/TRAINEE:/BIN/BASH VBOXADD:X:992:1::/VAR/RUN/VBOXADD:/BIN/FALSE TSS:X:59:59:ACCOUNT USED BY THE TROUSERS PACKAGE TO SANDBOX THE TCSD DAEMON:/DEV/NULL:/SBIN/NOLOGIN
Command Line Switches
To do : Use the –help option of the tr command to view the command line switches.
The paste Command
The paste command concatenates lines from n files. For example:
[root@centos7 ~]# paste -d: /etc/passwd /etc/shadow root:x:0:0:root:/root:/bin/bash:root:$6$r4px/s9L2uwGSFnI$NkK5mzNF.CMAFFqMcO.i.tnrMZQDkriDLYwICsimsPaDWKFwUHS3NhDwZY5e7P3glIu.gTBtaOE.SOOW/D.AU/:16502:0:99999:7::: bin:x:1:1:bin:/bin:/sbin/nologin:bin:*:16231:0:99999:7::: daemon:x:2:2:daemon:/sbin:/sbin/nologin:daemon:*:16231:0:99999:7::: adm:x:3:4:adm:/var/adm:/sbin/nologin:adm:*:16231:0:99999:7::: lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin:lp:*:16231:0:99999:7::: sync:x:5:0:sync:/sbin:/bin/sync:sync:*:16231:0:99999:7::: shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown:shutdown:*:16231:0:99999:7::: halt:x:7:0:halt:/sbin:/sbin/halt:halt:*:16231:0:99999:7::: mail:x:8:12:mail:/var/spool/mail:/sbin/nologin:mail:*:16231:0:99999:7::: operator:x:11:0:operator:/root:/sbin/nologin:operator:*:16231:0:99999:7::: games:x:12:100:games:/usr/games:/sbin/nologin:games:*:16231:0:99999:7::: ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin:ftp:*:16231:0:99999:7::: nobody:x:99:99:Nobody:/:/sbin/nologin:nobody:*:16231:0:99999:7::: dbus:x:81:81:System message bus:/:/sbin/nologin:dbus:!!:16502:::::: polkitd:x:999:998:User for polkitd:/:/sbin/nologin:polkitd:!!:16502:::::: unbound:x:998:997:Unbound DNS resolver:/etc/unbound:/sbin/nologin:unbound:!!:16502:::::: colord:x:997:996:User for colord:/var/lib/colord:/sbin/nologin:colord:!!:16502:::::: usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin:usbmuxd:!!:16502:::::: avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin:avahi:!!:16502:::::: avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin:avahi-autoipd:!!:16502:::::: saslauth:x:996:76:"Saslauthd user":/run/saslauthd:/sbin/nologin:saslauth:!!:16502:::::: qemu:x:107:107:qemu user:/:/sbin/nologin:qemu:!!:16502:::::: libstoragemgmt:x:995:994:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin:libstoragemgmt:!!:16502:::::: rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin:rpc:!!:16502:0:99999:7::: rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin:rpcuser:!!:16502:::::: nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin:nfsnobody:!!:16502:::::: rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin:rtkit:!!:16502:::::: radvd:x:75:75:radvd user:/:/sbin/nologin:radvd:!!:16502:::::: ntp:x:38:38::/etc/ntp:/sbin/nologin:ntp:!!:16502:::::: chrony:x:994:993::/var/lib/chrony:/sbin/nologin:chrony:!!:16502:::::: abrt:x:173:173::/etc/abrt:/sbin/nologin:abrt:!!:16502:::::: pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin:pulse:!!:16502:::::: gdm:x:42:42::/var/lib/gdm:/sbin/nologin:gdm:!!:16502:::::: gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin:gnome-initial-setup:!!:16502:::::: postfix:x:89:89::/var/spool/postfix:/sbin/nologin:postfix:!!:16502:::::: sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin:sshd:!!:16502:::::: tcpdump:x:72:72::/:/sbin/nologin:tcpdump:!!:16502:::::: trainee:x:1000:1000:trainee:/home/trainee:/bin/bash:trainee:$6$tMd44tmmFiitAS7.$sJSua3jhyKm2k0mIifYuTpUO0d6q6/gS3PDyuxbHadHVYLsoVslZ3Pn8m5X93rr64oj.KK8OL6J.gvhxbQBrZ1:16502:0:99999:7::: vboxadd:x:992:1::/var/run/vboxadd:/bin/false:vboxadd:!!:16691:::::: tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin:tss:!!:16723::::::
Important: Note that you need to become root to be able to execute this command. The reason is that normal users do not have the right to read the /etc/shadow file.
Command Line Switches
To do : Use the –help option of the paste command to view the command line switches.
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@centos7 ~]# dd if=/dev/zero of=/file bs=1024k count=250 250+0 records in 250+0 records out 262144000 bytes (262 MB) copied, 0.215447 s, 1.2 GB/s
Now use the split command to divide the file into 5 smaller files each of 50:
[root@centos7 ~]# split -b 50m /file filepart [root@centos7 ~]# ls -l | grep filepart -rw-r--r--. 1 root root 52428800 Oct 19 15:31 filepartaa -rw-r--r--. 1 root root 52428800 Oct 19 15:31 filepartab -rw-r--r--. 1 root root 52428800 Oct 19 15:31 filepartac -rw-r--r--. 1 root root 52428800 Oct 19 15:31 filepartad -rw-r--r--. 1 root root 52428800 Oct 19 15:31 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@centos7 ~]# cat fileparta* > newfile [root@centos7 ~]# ls -l | grep newf -rw-r--r--. 1 root root 262144000 Oct 19 15:31 newfile
Command Line Switches
To do : Use the –help option of the split command to view the command line switches.
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@centos ~]# 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 is great!
Now compare the two files:
[root@centos7 ~]# diff /etc/passwd /root/passwd 38,39c38 < trainee:x:1000:1000:trainee:/home/trainee:/bin/bash < vboxadd:x:992:1::/var/run/vboxadd:/bin/false --- > trainee10:x:1000:1000:trainee:/home/trainee:/bin/bash 40a40 > Linux is great!
In this output you will notice the < and > characters. The first makes reference to the first file, /etc/passwd, whist the second makes reference to the second file, /root/passwd.
The output 38,39c38 means that line 39 needs to be changed in /etc/passwd so that is the same as line 38 in /root/passwd.
The output 40a40 means that at line 40 in /etc/passwd line 40 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.
The cmp Command
The cmp command compares two files character by character. By default, the command stops after finding the first difference:
[root@centos7 ~]# cmp /root/passwd /etc/passwd /root/passwd /etc/passwd differ: byte 1873, line 38
The -l switch shows all of the differences in a three column format:
[root@centos7 ~]# cmp -l /root/passwd /etc/passwd | more cmp: EOF on /root/passwd 1873 61 72 1874 60 170 1876 170 61 1877 72 60 1878 61 60 1880 60 72 1881 60 61 1882 72 60 1883 61 60 1885 60 72 1886 60 164 1887 72 162 1888 164 141 1889 162 151 1890 141 156 1891 151 145 1892 156 145 1893 145 72 1894 145 57 1895 72 150 1896 57 157 1897 150 155 1898 157 145 1899 155 57 1900 145 164 1901 57 162 1902 164 141 1903 162 151 --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.
The patch Command
The patch command is used to apply modifications contained within a patch file to an older version of a file so that it becomes the newer version of the file.
The patch command is not installed by default in RHEL/CentOS 7:
[root@centos7 ~]# yum install patch Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirror.ibcp.fr * extras: mirrors.atosworldline.com * updates: mirror.ibcp.fr Resolving Dependencies --> Running transaction check ---> Package patch.x86_64 0:2.7.1-8.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ====================================================================================================================================================== Package Arch Version Repository Size ====================================================================================================================================================== Installing: patch x86_64 2.7.1-8.el7 base 110 k Transaction Summary ====================================================================================================================================================== Install 1 Package Total download size: 110 k Installed size: 210 k Is this ok [y/d/N]: y Downloading packages: patch-2.7.1-8.el7.x86_64.rpm | 110 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : patch-2.7.1-8.el7.x86_64 1/1 Verifying : patch-2.7.1-8.el7.x86_64 1/1 Installed: patch.x86_64 0:2.7.1-8.el7 Complete!
If you recall, you made some changes to the original greptest file that you downloaded:
[root@centos7 ~]# cat /tmp/greptest # Starting comment ^ This line will be used to demonstrate the use of fgrep fenestrOS fenestros # Another comment 555-5555 f .fenestros .fe £ # End comment
You also used egrep to remove all the comments and empty lines and save the result to /tmp/greptest1:
[root@centos ~]# 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@centos7 ~]# cd /tmp [root@centos7 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@centos tmp]# cat /tmp/greptest.patch --- greptest 2013-01-07 15:14:03.646565259 +0100 +++ greptest1 2013-01-07 15:07:05.657375635 +0100 @@ -1,14 +1,7 @@ -# Starting comment -^ This line will be used to demonstrate the use of fgrep fenestrOS fenestros -# Another comment 555-5555 f - .fenestros - .fe - £ -# End comment
Now apply the patch file:
[root@centos7 tmp]# patch < greptest.patch patching file greptest
Finally, check the contents of the patched greptest file:
[root@centos7 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.
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@centos7 tmp]# strings /usr/bin/passwd | more /lib64/ld-linux-x86-64.so.2 {1~` libuser.so.1 g_value_get_int64 is_selinux_enabled _ITM_deregisterTMCloneTable g_free g_value_array_get_nth __gmon_start__ g_value_get_string g_type_check_value_holds g_value_get_long _Jv_RegisterClasses freecon _ITM_registerTMCloneTable lu_ent_set_string lu_ent_get_first_value_strdup lu_error_free lu_user_lock _fini _init lu_strerror lu_ent_free lu_ent_new lu_user_modify lu_prompt_console lu_start lu_user_lookup_name --More--
Print the offset within the file before each string:
[root@centos7 tmp]# strings -t d /usr/bin/passwd | more 568 /lib64/ld-linux-x86-64.so.2 648 {1~` 2833 libuser.so.1 2846 g_value_get_int64 2864 is_selinux_enabled 2883 _ITM_deregisterTMCloneTable 2911 g_free 2918 g_value_array_get_nth 2940 __gmon_start__ 2955 g_value_get_string 2974 g_type_check_value_holds 2999 g_value_get_long 3016 _Jv_RegisterClasses 3036 freecon 3044 _ITM_registerTMCloneTable 3070 lu_ent_set_string 3088 lu_ent_get_first_value_strdup 3118 lu_error_free 3132 lu_user_lock 3145 _fini 3151 _init 3157 lu_strerror 3169 lu_ent_free 3181 lu_ent_new 3192 lu_user_modify 3207 lu_prompt_console 3225 lu_start 3234 lu_user_lookup_name --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@centos7 tmp]# strings -t d -n 15 /usr/bin/passwd | more 568 /lib64/ld-linux-x86-64.so.2 2846 g_value_get_int64 2864 is_selinux_enabled 2883 _ITM_deregisterTMCloneTable 2918 g_value_array_get_nth 2955 g_value_get_string 2974 g_type_check_value_holds 2999 g_value_get_long 3016 _Jv_RegisterClasses 3044 _ITM_registerTMCloneTable 3070 lu_ent_set_string 3088 lu_ent_get_first_value_strdup 3207 lu_prompt_console 3234 lu_user_lookup_name 3261 lu_ent_set_long 3303 lu_user_removepass 3322 libgobject-2.0.so.0 3342 libglib-2.0.so.0 3372 poptSetOtherOptionHelp 3464 poptHelpOptions 3503 audit_log_acct_message 3599 libpam_misc.so.0 3640 audit_log_user_avc_message 3667 audit_log_user_message 3690 libselinux.so.1 3706 selinux_set_callback 3727 selinux_check_access 3759 libpthread.so.0 --More--
The -f switch prints the name of the file before each string:
[root@centos7 tmp]# strings -f /bin/* | grep "(c)" /bin/btrace: # Copyright (c) 2005 Silicon Graphics, Inc. /bin/cdda-player: (c) 1997,98 Gerd Knorr <kraxel@goldbach.in-berlin.de> /bin/cdda-player: (c) 2005, 2006 Rocky Bernstein <rocky@gnu.org> /bin/cd-drive: Copyright (c) 2003-2005, 2007-2008, 2011-2013 R. Bernstein /bin/cd-info: Copyright (c) 2003-2005, 2007-2008, 2011-2013 R. Bernstein /bin/cd-read: Copyright (c) 2003-2005, 2007-2008, 2011-2013 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/crash: (c) a pointer to the first item pointed to by the start address. /bin/crash: (c) a list_head that is embedded within a data structure that is /bin/crash: switch(c) /bin/csh: @(#) Copyright (c) 1991 The Regents of the University of California. /bin/diffpp: # Copyright (c) 1996-1998 Markku Rossi /bin/drill: Copyright (c) 2004-2008 NLnet Labs. /bin/festival_server: ## Copyright (c) 1996 ## /bin/festival_server_control: ## Copyright (c) 1996 ## /bin/gcm-calibrate: Copyright (c) /bin/gprof: @(#) Copyright (c) 1983 Regents of the University of California. /bin/hpijs: Copyright (c) 2001-2004, Hewlett-Packard Co. /bin/ibus-setup: # Copyright (c) 2007-2010 Peng Huang <shawn.p.huang@gmail.com> /bin/ibus-setup: # Copyright (c) 2007-2010 Red Hat, Inc. /bin/ibus-table-createdb: # Copyright (c) 2008-2009 Yu Yuwei <acevery@gmail.com> /bin/iso-info: Copyright (c) 2003-2005, 2007-2008, 2011-2013 R. Bernstein /bin/iso-read: Copyright (c) 2003-2005, 2007-2008, 2011-2013 R. Bernstein /bin/lsusb.py: # (c) Kurt Garloff <garloff@suse.de>, 2/2009, GPL v2 or v3. /bin/mmc-tool: Copyright (c) 2003-2005, 2007-2008, 2011-2013 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/ping: @(#) Copyright (c) 1989 The Regents of the University of California. /bin/ping6: @(#) Copyright (c) 1989 The Regents of the University of California. /bin/pod2usage: # Copyright (c) 1996-2000 by Bradford Appleton. All rights reserved. /bin/qemu-img: qemu-img version 1.5.3, Copyright (c) 2004-2008 Fabrice Bellard /bin/repoclosure: # seth vidal 2005 (c) etc etc /bin/repodiff: # (c) 2007 Red Hat. Written by skvidal@fedoraproject.org /bin/repo-graph: # (c) 2005 Panu Matilainen <pmatilai@laiskiainen.org> /bin/repomanage: # (c) Copyright Seth Vidal 2004 /bin/repoquery: # (c) pmatilai@laiskiainen.org /bin/repo-rss: # seth vidal 2005 (c) etc etc /bin/repotrack: # (c) 2005 seth vidal skvidal at phy.duke.edu /bin/rngtest: Copyright (c) 2004 by Henrique de Moraes Holschuh /bin/saytime: ;; Copyright (c) 1996,1997 ;; /bin/sbcdec: Copyright (c) 2004-2010 Marcel Holtmann /bin/sbcenc: Copyright (c) 2004-2010 Marcel Holtmann /bin/screen: Copyright (c) 2010 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-2002, 2003, 2005, 2006, 2007 Juergen Weigert, Michael Schroeder /bin/screen: Copyright (c) 1987 Oliver Laumann /bin/sliceprint: # Copyright (c) 1996-1999 Markku Rossi /bin/soundstretch: Copyright (c) Olli Parviainen /bin/ssh-copy-id: # Copyright (c) 1999-2013 Philip Hands <phil@hands.com> /bin/tcptraceroute: # Copyright (c) 2007 Dmitry Butskoy /bin/tcsh: @(#) Copyright (c) 1991 The Regents of the University of California. /bin/text2wave: ;; Copyright (c) 1996,1997 ;; /bin/traceroute: Copyright (c) 2008 Dmitry Butskoy, License: GPL v2 or any later /bin/traceroute6: Copyright (c) 2008 Dmitry Butskoy, License: GPL v2 or any later /bin/verifytree: # copyright (c) 2008 Red Hat, Inc - written by Seth Vidal and Will Woods /bin/wavpack: Copyright (c) 1998 - 2009 Conifer Software. All Rights Reserved. /bin/wvdial: Copyright (c) 1997-2005 Net Integration Technologies, Inc. /bin/wvdialconf: Copyright (c) 1997-2005 Net Integration Technologies, Inc. /bin/wvgain: Copyright (c) 2005 - 2009 Conifer Software. All Rights Reserved. /bin/wvunpack: Copyright (c) 1998 - 2009 Conifer Software. All Rights Reserved. /bin/yum-debug-dump: ## (c) 2008 Red Hat. Written by skvidal@fedoraproject.org /bin/yum-debug-restore: ## (c) 2008 Red Hat. Written by skvidal@fedoraproject.org /bin/zip: ight (c)H /bin/zip: Copyright (c) 1990-2008 Info-ZIP. All rights reserved. /bin/zip: Copyright (c) 1990-2008 Info-ZIP - Type '%s "-L"' for software license. /bin/zipcloak: Copyright (c) 1990-2008 Info-ZIP. All rights reserved. /bin/zipcloak: Copyright (c) 1990-2008 Info-ZIP - Type '%s "-L"' for software license. /bin/zipnote: Copyright (c) 1990-2008 Info-ZIP. All rights reserved. /bin/zipnote: Copyright (c) 1990-2008 Info-ZIP - Type '%s "-L"' for software license. /bin/zipsplit: Copyright (c) 1990-2008 Info-ZIP. All rights reserved. /bin/zipsplit: Copyright (c) 1990-2008 Info-ZIP - Type '%s "-L"' for software license.
Command Line Switches
To do : Use the –help option of the strings command to view the command line switches.
The comm Command
This command compares two text files and prints the differences to standard output:
[root@centos7 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:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin unbound:x:998:997:Unbound DNS resolver:/etc/unbound:/sbin/nologin colord:x:997:996:User for colord:/var/lib/colord:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin saslauth:x:996:76:"Saslauthd user":/run/saslauthd:/sbin/nologin qemu:x:107:107:qemu user:/:/sbin/nologin libstoragemgmt:x:995:994:daemon account for libstoragemgmt:/var/run/lsm:/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 nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin radvd:x:75:75:radvd user:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin chrony:x:994:993::/var/lib/chrony:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin trainee10:x:1000:1000:trainee:/home/trainee:/bin/bash trainee:x:1000:1000:trainee:/home/trainee:/bin/bash tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin comm: file 2 is not in sorted order Linux est super! vboxadd:x:992:1::/var/run/vboxadd:/bin/false comm: file 1 is not in sorted order tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/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@centos7 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:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin unbound:x:998:997:Unbound DNS resolver:/etc/unbound:/sbin/nologin colord:x:997:996:User for colord:/var/lib/colord:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin saslauth:x:996:76:"Saslauthd user":/run/saslauthd:/sbin/nologin qemu:x:107:107:qemu user:/:/sbin/nologin libstoragemgmt:x:995:994:daemon account for libstoragemgmt:/var/run/lsm:/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 nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin radvd:x:75:75:radvd user:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin chrony:x:994:993::/var/lib/chrony:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/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.
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@centos7 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@centos7 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:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin
The command can also be used to display the first y bytes of a file by using the -c switch:
[root@centos7 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@centos7 tmp]#
If the value of y is negative, head displays all bytes in the file except the last y bytes:
lp:x:4:7[root@centos7 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:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin unbound:x:998:997:Unbound DNS resolver:/etc/unbound:/sbin/nologin colord:x:997:996:User for colord:/var/lib/colord:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin saslauth:x:996:76:"Saslauthd user":/run/saslauthd:/sbin/nologin qemu:x:107:107:qemu user:/:/sbin/nologin libstoragemgmt:x:995:994:daemon account for libstoragemgmt:/var/run/lsm:/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 nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin radvd:x:75:75:radvd user:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin chrony:x:994:993::/var/lib/chrony:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin trainee:x:1000:1000:trainee:/home/trainee:/bin/[root@centos7 tmp]#
Both x and y can accept multipliers:
[root@centos7 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:99:99:Nobody:/:/sbin/nologin[root@centos7 tmp]# [root@centos7 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:99:99:Nobody:/:/sbin/nologin[root@centos7 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.
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@centos7 tmp]# tail /etc/passwd abrt:x:173:173::/etc/abrt:/sbin/nologin pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin trainee:x:1000:1000:trainee:/home/trainee:/bin/bash vboxadd:x:992:1::/var/run/vboxadd:/bin/false tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
You can change the default value of x by using the -n switch:
[root@centos7 tmp]# tail -n 15 /etc/passwd nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin radvd:x:75:75:radvd user:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin chrony:x:994:993::/var/lib/chrony:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin trainee:x:1000:1000:trainee:/home/trainee:/bin/bash vboxadd:x:992:1::/var/run/vboxadd:/bin/false tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
The command can also be used to display the last y bytes of a file by using the -c switch:
[root@centos7 tmp]# tail -c 150 /etc/passwd bash vboxadd:x:992:1::/var/run/vboxadd:/bin/false tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
If the value of y is positive, tail displays all bytes in the file after the yth byte:
[root@centos7 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:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin unbound:x:998:997:Unbound DNS resolver:/etc/unbound:/sbin/nologin colord:x:997:996:User for colord:/var/lib/colord:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin saslauth:x:996:76:"Saslauthd user":/run/saslauthd:/sbin/nologin qemu:x:107:107:qemu user:/:/sbin/nologin libstoragemgmt:x:995:994:daemon account for libstoragemgmt:/var/run/lsm:/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 nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin radvd:x:75:75:radvd user:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin chrony:x:994:993::/var/lib/chrony:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin trainee:x:1000:1000:trainee:/home/trainee:/bin/bash vboxadd:x:992:1::/var/run/vboxadd:/bin/false tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
Both x and y can accept multipliers:
[root@centos7 tmp]# tail -c 1b /etc/passwd PulseAudio System Daemon:/var/run/pulse:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin trainee:x:1000:1000:trainee:/home/trainee:/bin/bash vboxadd:x:992:1::/var/run/vboxadd:/bin/false tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin [root@centos7 tmp]# tail -c 512 /etc/passwd PulseAudio System Daemon:/var/run/pulse:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin trainee:x:1000:1000:trainee:/home/trainee:/bin/bash vboxadd:x:992:1::/var/run/vboxadd:/bin/false tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/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@centos7 tmp]# tail -f /var/log/messages Oct 19 15:58:06 centos7 dbus-daemon: dbus[542]: [system] Activating service name='org.freedesktop.PackageKit' (using servicehelper) Oct 19 15:58:06 centos7 dbus[542]: [system] Activating service name='org.freedesktop.PackageKit' (using servicehelper) Oct 19 15:58:06 centos7 dbus-daemon: dbus[542]: [system] Successfully activated service 'org.freedesktop.PackageKit' Oct 19 15:58:06 centos7 dbus[542]: [system] Successfully activated service 'org.freedesktop.PackageKit' Oct 19 16:00:01 centos7 systemd: Created slice user-0.slice. Oct 19 16:00:01 centos7 systemd: Starting Session 17 of user root. Oct 19 16:00:01 centos7 systemd: Started Session 17 of user root. Oct 19 16:01:01 centos7 systemd: Created slice user-0.slice. Oct 19 16:01:01 centos7 systemd: Starting Session 18 of user root. Oct 19 16:01:01 centos7 systemd: Started Session 18 of user root. ^C
Command Line Switches
To do : Use the –help option of the head command to view the command line switches.
LAB #4 - Use the grep, tr and cut to extract your IP address from the output of ifconfig
[root@centos7 tmp]# ifconfig enp0s3 enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255 inet6 fe80::a00:27ff:febd:f523 prefixlen 64 scopeid 0x20<link> ether 08:00:27:bd:f5:23 txqueuelen 1000 (Ethernet) RX packets 7462 bytes 6158140 (5.8 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4130 bytes 555287 (542.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@centos7 tmp]# ifconfig enp0s3 | grep "inet" inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255 inet6 fe80::a00:27ff:febd:f523 prefixlen 64 scopeid 0x20<link> [root@centos7 tmp]# ifconfig enp0s3 | grep "inet" | grep -v "inet6" inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255 [root@centos7 tmp]# ifconfig enp0s3 | grep "inet" | grep -v "inet6" | tr -s " " ":" :inet:10.0.2.15:netmask:255.255.255.0:broadcast:10.0.2.255 [root@centos7 tmp]# ifconfig enp0s3 | grep "inet" | grep -v "inet6" | tr -s " " ":" | cut -d: -f3 10.0.2.15
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.
<html>
Copyright © 2004-2019 Hugh Norris.<br><br>
</html>