Last updated on: 2020/01/30 03:28
[stextbox id='black' image='null'] To do - You are currently the root user in your terminal. Before proceeding further, type exit and hit the ↵ Enter key. [/stextbox]
Using this command with the -a switch allows you to identify which combination of keys should be used to control a foreground process:
trainee@SLES12SP1:~> stty -a speed 38400 baud; rows 24; columns 80; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; 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
[stextbox id='black' image='null'] The two most important combinations are intr = ^C and susp = ^Z. The former kills the process whilst the latter suspends its execution. [/stextbox]
[stextbox id='black' image='null'] To do : Use the –help option of the stty command to view the command line switches. [/stextbox]
This command's output gives the current system date and time. The command can also be used to set the system date:
trainee@SLES12SP1:~> date Fri 30 Sep 15:19:54 CEST 2016
[stextbox id='black' image='null'] To do : Use the –help option of the date command to view the command line switches. [/stextbox]
This command's output shows who is currently connected to the system:
trainee@SLES12SP1:~> who trainee pts/0 2016-09-30 10:24 (10.0.2.2)
[stextbox id='black' image='null'] To do : Use the –help option of the who command to view the command line switches. [/stextbox]
This command's output shows the free space on each mounted block device:
trainee@SLES12SP1:~> df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 18867200 3510848 15185952 19% / devtmpfs 188376 0 188376 0% /dev tmpfs 197260 0 197260 0% /dev/shm tmpfs 197260 5716 191544 3% /run tmpfs 197260 0 197260 0% /sys/fs/cgroup /dev/sda2 18867200 3510848 15185952 19% /.snapshots /dev/sda2 18867200 3510848 15185952 19% /var/lib/mariadb /dev/sda2 18867200 3510848 15185952 19% /var/lib/libvirt/images /dev/sda2 18867200 3510848 15185952 19% /var/lib/mailman /dev/sda2 18867200 3510848 15185952 19% /var/tmp /dev/sda2 18867200 3510848 15185952 19% /var/lib/pgsql /dev/sda2 18867200 3510848 15185952 19% /usr/local /dev/sda2 18867200 3510848 15185952 19% /tmp /dev/sda2 18867200 3510848 15185952 19% /var/spool /dev/sda2 18867200 3510848 15185952 19% /srv /dev/sda2 18867200 3510848 15185952 19% /var/opt /dev/sda2 18867200 3510848 15185952 19% /opt /dev/sda2 18867200 3510848 15185952 19% /var/log /dev/sda2 18867200 3510848 15185952 19% /var/lib/mysql /dev/sda2 18867200 3510848 15185952 19% /home /dev/sda2 18867200 3510848 15185952 19% /var/crash /dev/sda2 18867200 3510848 15185952 19% /var/lib/named /dev/sda2 18867200 3510848 15185952 19% /boot/grub2/x86_64-efi /dev/sda2 18867200 3510848 15185952 19% /boot/grub2/i386-pc
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@SLES12SP1:~> df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 18G 3.4G 15G 19% / devtmpfs 184M 0 184M 0% /dev tmpfs 193M 0 193M 0% /dev/shm tmpfs 193M 5.6M 188M 3% /run tmpfs 193M 0 193M 0% /sys/fs/cgroup /dev/sda2 18G 3.4G 15G 19% /.snapshots /dev/sda2 18G 3.4G 15G 19% /var/lib/mariadb /dev/sda2 18G 3.4G 15G 19% /var/lib/libvirt/images /dev/sda2 18G 3.4G 15G 19% /var/lib/mailman /dev/sda2 18G 3.4G 15G 19% /var/tmp /dev/sda2 18G 3.4G 15G 19% /var/lib/pgsql /dev/sda2 18G 3.4G 15G 19% /usr/local /dev/sda2 18G 3.4G 15G 19% /tmp /dev/sda2 18G 3.4G 15G 19% /var/spool /dev/sda2 18G 3.4G 15G 19% /srv /dev/sda2 18G 3.4G 15G 19% /var/opt /dev/sda2 18G 3.4G 15G 19% /opt /dev/sda2 18G 3.4G 15G 19% /var/log /dev/sda2 18G 3.4G 15G 19% /var/lib/mysql /dev/sda2 18G 3.4G 15G 19% /home /dev/sda2 18G 3.4G 15G 19% /var/crash /dev/sda2 18G 3.4G 15G 19% /var/lib/named /dev/sda2 18G 3.4G 15G 19% /boot/grub2/x86_64-efi /dev/sda2 18G 3.4G 15G 19% /boot/grub2/i386-pc
[stextbox id='black' image='null'] To do : Use the –help option of the df command to view the command line switches. [/stextbox]
This command's output shows the memory usage:
trainee@SLES12SP1:~> free total used free shared buffers cached Mem: 394524 385872 8652 5120 452 296176 -/+ buffers/cache: 89244 305280 Swap: 2103292 600 2102692
The units are shown as blocks. In order to humanize the output, it is possible to use the -h switch:
trainee@SLES12SP1:~> free -h total used free shared buffers cached Mem: 385M 376M 8.4M 5.0M 452K 289M -/+ buffers/cache: 87M 298M Swap: 2.0G 600K 2.0G
[stextbox id='black' image='null'] To do : Use the –help option of the free command to view the command line switches. [/stextbox]
This command's output indicates the user name associated with the current effective user ID:
trainee@SLES12SP1:~> whoami trainee
Now become the system administrator root:
trainee@SLES12SP1:~> su - Password: fenestros SLES12SP1:~ #
[stextbox id='black' image='null'] Important : Note that the password will not be visible. [/stextbox]
Now use the whoami command again:
SLES12SP1:~ # whoami root SLES12SP1:~ #
[stextbox id='black' image='null'] Important : Note the current effective user ID is root. [/stextbox]
Finally execute the exit command to return as trainee:
SLES12SP1:~ # exit logout trainee@SLES12SP1:~>
[stextbox id='black' image='null'] To do : Use the –help option of the whoami command to view the command line switches. [/stextbox]
This command's output shows the current working directory:
trainee@SLES12SP1:~> pwd /home/trainee
[stextbox id='black' image='null'] To do : Use the help command with pwd option to view the command line switches. [/stextbox]
This command's output changes the current working directory to that specified by the argument:
trainee@SLES12SP1:~> cd /tmp trainee@SLES12SP1:/tmp> pwd /tmp trainee@SLES12SP1:/tmp>
[stextbox id='black' image='null'] To do : Use the help command with cd option to view the command line switches. [/stextbox]
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@SLES12SP1:/tmp> ls hsperfdata_root inode managera1411267841657715235client managera3336001029897679475server managera4847938942232964844client managera5050357016347721452server systemd-private-04f820fa26c745be8ddba814c6292f21-rtkit-daemon.service-o4lKP5 systemicontmp5578677472245134133dat systemicontmp7082392205020802884dat
[stextbox id='black' image='null'] To do : Use the –help option of the ls command to view the command line switches. [/stextbox]
This command's output shows information about open files:
trainee@SLES12SP1:/tmp> su - Password: SLES12SP1:~ # lsof | more COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME systemd 1 root cwd DIR 0,34 156 256 / systemd 1 root rtd DIR 0,34 156 256 / systemd 1 root txt REG 0,34 1218976 74832 /usr/lib/systemd/systemd systemd 1 root mem REG 0,34 18904 23416 /lib64/libdl-2.19.so systemd 1 root mem REG 0,34 416064 24152 /usr/lib64/libpcre.so.1.2.1 systemd 1 root mem REG 0,34 1974416 23410 /lib64/libc-2.19.so systemd 1 root mem REG 0,34 137440 23438 /lib64/libpthread-2.19.so systemd 1 root mem REG 0,34 42672 23442 /lib64/librt-2.19.so systemd 1 root mem REG 0,34 150128 24032 /usr/lib64/libseccomp.so.2.1.0 systemd 1 root mem REG 0,34 43960 25095 /lib64/libapparmor.so.1.0.4 systemd 1 root mem REG 0,34 84864 24581 /usr/lib64/libkmod.so.2.2.7 systemd 1 root mem REG 0,34 18976 24919 /lib64/libcap.so.2.22 systemd 1 root mem REG 0,34 100312 24963 /usr/lib64/libaudit.so.1.0.0 systemd 1 root mem REG 0,34 60096 59333 /lib64/libpam.so.0.83.1 systemd 1 root mem REG 0,34 40880 23873 /lib64/libwrap.so.0.7.6 systemd 1 root mem REG 0,34 138792 25858 /lib64/libselinux.so.1 systemd 1 root mem REG 0,34 158192 23402 /lib64/ld-2.19.so systemd 1 root 0u CHR 1,3 0t0 4298 /dev/null systemd 1 root 1u CHR 1,3 0t0 4298 /dev/null systemd 1 root 2u CHR 1,3 0t0 4298 /dev/null systemd 1 root 3r 0000 0,10 0 4268 anon_inode systemd 1 root 4u 0000 0,10 0 4268 anon_inode --More--
[stextbox id='black' image='null'] To do : Use the –help option of the lsof command to view the command line switches. [/stextbox]
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:
SLES12SP1:~ # exit logout trainee@SLES12SP1:/tmp> touch test trainee@SLES12SP1:/tmp> ls hsperfdata_root inode managera1411267841657715235client managera3336001029897679475server managera4847938942232964844client managera5050357016347721452server systemd-private-04f820fa26c745be8ddba814c6292f21-rtkit-daemon.service-o4lKP5 systemicontmp5578677472245134133dat systemicontmp7082392205020802884dat test
[stextbox id='black' image='null'] To do : Use the –help option of the touch command to view the command line switches. [/stextbox]
This command writes the arguments to the standard output (i.e. the screen):
trainee@SLES12SP1:/tmp> echo fenestros fenestros
[stextbox id='black' image='null'] To do : Use the help command with echo option to view the command line switches. [/stextbox]
This command is used to copy a source to a destination or multiple sources to a directory:
trainee@SLES12SP1:/tmp> cp test ~ trainee@SLES12SP1:/tmp> ls -l ~ total 4 drwxr-xr-x 1 trainee users 0 May 1 15:58 bin drwxr-xr-x 1 trainee users 0 May 2 16:11 Desktop drwxr-xr-x 1 trainee users 0 May 2 16:11 Documents drwxr-xr-x 1 trainee users 0 May 2 16:11 Downloads drwxr-xr-x 1 trainee users 0 May 2 16:11 Music drwxr-xr-x 1 trainee users 0 May 2 16:11 Pictures drwxr-xr-x 1 trainee users 0 May 2 16:11 Public drwxr-xr-x 1 trainee users 20 May 1 15:58 public_html drwxr-xr-x 1 trainee users 0 May 2 16:11 Templates -rw-r--r-- 1 trainee users 0 Oct 1 06:01 test drwxr-xr-x 1 trainee users 0 May 2 16:11 Videos -rw-r--r-- 1 trainee users 391 Sep 30 10:27 vitext
[stextbox id='black' image='null'] Note the use of the ~ (tilde) character which is a shortcut to the current user's home directory. In the case of this example : /home/trainee. [/stextbox]
[stextbox id='black' image='null'] To do : Use the –help option of the cp command to view the command line switches. [/stextbox]
This command determines a file type:
trainee@SLES12SP1:/tmp> file ~/test /home/trainee/test: empty
[stextbox id='black' image='null'] Important - Note that in the case of the first line of the above output, tha command file is incapable of informing you of the type of file since test is empty. [/stextbox]
Using the > key, redirect the output of echo fenestros into the /home/trainee/test file as follows:
trainee@SLES12SP1:/tmp> echo "fenestros" > ~/test
Now use the file command once again to determine the file type:
trainee@SLES12SP1:/tmp> file ~/test /home/trainee/test: ASCII text
[stextbox id='black' image='null'] To do : Use the –help option of the file command to view the command line switches. [/stextbox]
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@SLES12SP1:/tmp> cat ~/test fenestros
[stextbox id='black' image='null'] To do : Use the –help option of the cat command to view the command line switches. [/stextbox]
This command renames a source to a destination or moves sources to a directory:
trainee@SLES12SP1:/tmp> mv test TeSt trainee@SLES12SP1:/tmp> ls -l total 12 drwxr-xr-x 1 root root 8 Sep 30 10:26 hsperfdata_root drwxr-xr-x 1 trainee users 48 Aug 18 13:14 inode -rwxrwxrwx 1 root root 50048 May 3 14:01 managera1411267841657715235client -rwxrwxrwx 1 root root 50048 May 3 14:01 managera3336001029897679475server -rwxrwxrwx 1 root root 50048 May 3 14:01 managera4847938942232964844client -rwxrwxrwx 1 root root 50048 May 3 14:01 managera5050357016347721452server drwx------ 1 root root 6 May 3 13:54 systemd-private-04f820fa26c745be8ddba814c6292f21-rtkit-daemon.service-o4lKP5 -rw-r--r-- 1 root root 0 May 3 14:01 systemicontmp5578677472245134133dat drwxr-xr-x 1 root root 0 May 3 14:01 systemicontmp7082392205020802884dat -rw-r--r-- 1 trainee users 10 Oct 1 06:06 TeSt
[stextbox id='black' image='null'] Note the use of the shortcut . which indicates the current working directory. [/stextbox]
[stextbox id='black' image='null'] To do : Use the –help option of the mv command to view the command line switches. [/stextbox]
This command creates the directory(ies) if it (they) does (do) not exist:
trainee@SLES12SP1:/tmp> cd ~ trainee@SLES12SP1:~> mkdir testdir trainee@SLES12SP1:~> ls bin Documents Music Public Templates Videos Desktop Downloads Pictures public_html testdir vitext
[stextbox id='black' image='null'] To do : Use the –help option of the mkdir command to view the command line switches. [/stextbox]
This command removes the directory(ies) if it (they) is (are) empty:
trainee@SLES12SP1:~> rmdir testdir trainee@SLES12SP1:~> ls bin Documents Music Public Templates vitext Desktop Downloads Pictures public_html Videos
[stextbox id='black' image='null'] To do : Use the –help option of the rmdir command to view the command line switches. [/stextbox]
This command removes a directory, empty of not, as well as files:
trainee@SLES12SP1:~> mkdir testdir1 trainee@SLES12SP1:~> cd /tmp trainee@SLES12SP1:/tmp> echo "fenestros" > TeSt trainee@SLES12SP1:/tmp> cd ~ trainee@SLES12SP1:~> mv /tmp/TeSt ~/testdir1 trainee@SLES12SP1:~> ls -lR testdir1/ testdir1/: total 4 -rw-r--r-- 1 trainee users 10 Oct 1 06:47 TeSt trainee@SLES12SP1:~> rmdir testdir1/ rmdir: failed to remove ‘testdir1/’: Directory not empty trainee@SLES12SP1:~> rm -rf testdir1/ trainee@SLES12SP1:~> ls bin Documents Music Public Templates vitext Desktop Downloads Pictures public_html Videos
[stextbox id='black' image='null'] To do : Use the –help option of the rm command to view the command line switches. [/stextbox]
This command writes a sorted concatenation of all files to standard output:
trainee@SLES12SP1:~> ls | sort aac abc bca bin Desktop Documents Downloads Music Pictures Public public_html Templates Videos vitext xyz trainee@SLES12SP1:~> ls | sort -r xyz vitext Videos Templates public_html Public Pictures Music Downloads Documents Desktop bin bca abc aac
[stextbox id='black' image='null'] Important - Note the use of the | character, called a pipe. A pipe is used to channel the standard output of the command that precedes it into the standard input of the command that follows it. [/stextbox]
[stextbox id='black' image='null'] To do : Use the –help option of the sort command to view the command line switches. [/stextbox]
This command is used to display a long file page by page:
trainee@SLES12SP1:~> more /etc/services # # Network services, Internet style # # Note that it is presently the policy of IANA to assign a single well-known # port number for both TCP and UDP; hence, most entries here have two entries # even if the protocol doesn't support UDP operations. # # This list could be found on: # http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbe rs.xml # # See also: services(5) # # # SERVICE NAME AND TRANSPORT PROTOCOL PORT NUMBER REGISTRY # # (last updated on 2011-11-23) # # ************************************************************************ # * Since we merged and converted this registry it loads more slowly. We * # * know it should load faster and are working on ways to achieve that. * # * Thank you for your patience. * # ************************************************************************ # --More--(0%)
[stextbox id='black' image='null'] Important - The ↵ Enter key scrolls down line by line, the Space Bar key scrolls down page by page and the Q key comes back to the command line prompt. [/stextbox]
[stextbox id='black' image='null'] To do : Use the –help option of the more command to view the command line switches. [/stextbox]
The less command 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@SLES12SP1:~> less /etc/services # # Network services, Internet style # # Note that it is presently the policy of IANA to assign a single well-known # port number for both TCP and UDP; hence, most entries here have two entries # even if the protocol doesn't support UDP operations. # # This list could be found on: # http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml # # See also: services(5) # # # SERVICE NAME AND TRANSPORT PROTOCOL PORT NUMBER REGISTRY # # (last updated on 2011-11-23) # # ************************************************************************ # * Since we merged and converted this registry it loads more slowly. We * # * know it should load faster and are working on ways to achieve that. * # * Thank you for your patience. * # ************************************************************************ /etc/services lines 1-22/16228 0%
[stextbox id='black' image='null'] To do : Use the –help option of the less command to view the command line switches. [/stextbox]
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@SLES12SP1:~> find acc find: ‘acc’: No such file or directory trainee@SLES12SP1:~> find aac aac
[stextbox id='black' image='null'] Important : Note that when the file cannot be found, the find command informs you clearly. However when the file is found, the find command just prints the name of the file to standard output. [/stextbox]
[stextbox id='black' image='null'] To do : Use the –help option of the find command to view the command line switches. [/stextbox]
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@SLES12SP1:~> su - Password: fenestros
[stextbox id='black' image='null'] Important : Note that the password will not be visible. [/stextbox]
[stextbox id='black' image='null'] To do : Use the –help option of the su command to view the command line switches. [/stextbox]
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:
SLES12SP1:~ # ls -l /var/lib/mlocate/mlocate.db -rw-r--r-- 1 root root 3804323 Sep 30 10:30 /var/lib/mlocate/mlocate.db
[stextbox id='black' image='null'] Important : For information concerning the database format, please see man 5 locatedb. [/stextbox]
The updatedb command is configured by editing the /etc/updatedb.conf file:
SLES12SP1:~ # cat /etc/updatedb.conf # /etc/updatedb.conf: config file for mlocate # This file sets variables that are used by updatedb. # For more info, see the updatedb.conf(5) manpage. # Filesystems that are pruned from updatedb database PRUNEFS="afs anon_inodefs auto autofs bdev binfmt binfmt_misc cgroup cifs coda configfs cramfs cpuset debugfs devfs devpts devtmps ecryptfs eventpollfs exofs futexfs ftpfs fuse fusectl gfs gfs2 hostfs hugetlbfs inotifyfs iso9660 jffs2 lustre misc mqueue ncpfs nfs NFS nfs4 nfsd nnpfs ocfs ocfs2 pipefs proc ramfs rpc_pipefs securityfs selinuxfs sfs shfs smbfs sockfs spufs sshfs subfs supermount sysfs tmpfs ubifs udf usbfs vboxsf vperfctrfs" # Paths which are pruned from updatedb database PRUNEPATHS="/tmp /var/tmp /var/cache /var/lock /var/run /var/spool /mnt /cdrom /usr/tmp /proc /media /sys /.snapshots /var/run/media" # Folder names that are pruned from updatedb database PRUNENAMES=".git .hg .svn CVS" # Skip bind mounts. PRUNE_BIND_MOUNTS="yes"
Use of these two commands is very simple:
SLES12SP1:~ # locate aac /lib/modules/3.12.49-11-default/kernel/drivers/scsi/aacraid /lib/modules/3.12.49-11-default/kernel/drivers/scsi/aacraid/aacraid.ko /usr/share/YaST2/scrconf/aaconf.scr /usr/share/mime/audio/aac.xml
[stextbox id='black' image='null'] To do : Use the –help option of the updatedb and locate commands to view their command line switches. [/stextbox]
This command is used to show the full paths of the executable, the configuration files and the manuals associated with the argument:
SLES12SP1:~ # whereis passwd passwd: /usr/bin/passwd /etc/passwd /etc/passwd.YaST2save /usr/share/man/man1/passwd.1ssl.gz /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz
[stextbox id='black' image='null'] To do : Use the –help option of the whereis command to view the command line switches. [/stextbox]
This command searches the PATH variable and returns to standard output the first full path associated with the argument:
SLES12SP1:~ # which passwd /usr/bin/passwd
[stextbox id='black' image='null'] To do : Use the –help option of the which command to view the command line switches. [/stextbox]
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:
SLES12SP1:~ # uptime 07:48am up 21:25, 1 user, load average: 0.10, 0.05, 0.05
The switches associated with this command are:
[stextbox id='black' image='null'] To do : Use the –help option of the uptime command to view the command line switches. [/stextbox]
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:
SLES12SP1:~ # w 07:57:55 up 21:34, 1 user, load average: 0.00, 0.03, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT trainee pts/0 10.0.2.2 04:09 0.00s 0.12s 0.00s sshd: trainee [priv]
The JCPU time is the time used by all processes attached to the tty. It does not include past background jobs, but does include currently running background jobs.
The PCPU time is the time used by the current process, named in the what field.
[stextbox id='black' image='null'] To do : Use the –help option of the w command to view the command line switches. [/stextbox]
This command prints system information to the standard output:
SLES12SP1:~ # uname -a Linux SLES12SP1 3.12.49-11-default #1 SMP Wed Nov 11 20:52:43 UTC 2015 (8d714a0) x86_64 x86_64 x86_64 GNU/Linux SLES12SP1:~ # uname -s Linux SLES12SP1:~ # uname -n SLES12SP1 SLES12SP1:~ # uname -r 3.12.49-11-default SLES12SP1:~ # uname -v #1 SMP Wed Nov 11 20:52:43 UTC 2015 (8d714a0) SLES12SP1:~ # uname -m x86_64 SLES12SP1:~ # uname -p x86_64 SLES12SP1:~ # uname -i x86_64 SLES12SP1:~ # uname -o GNU/Linux
[stextbox id='black' image='null'] To do : Use the –help option of the uname command to view the command line switches. [/stextbox]
This command summarizes disk usage of each file, recursively for directories:
SLES12SP1:~ # du -sh /* 2>/dev/null 5.1M /bin 44M /boot 0 /dev 19M /etc 216K /home 190M /lib 16M /lib64 0 /mnt 151M /opt 0 /proc 6.1M /root 5.4M /run 11M /sbin 0 /selinux 64K /srv 0 /sys 60K /tmp 2.6G /usr 133M /var
[stextbox id='black' image='null'] Important : Note the use of the 2>/dev/null redirection. This sends all eventual errors in the file descriptor 2 directly to /dev/null so that they do not appear in the output. File descriptors are covered in the The Command Line Interface unit. [/stextbox]
[stextbox id='black' image='null'] To do : Use the –help option of the du command to view the command line switches. [/stextbox]
This command is used to clear the current screen of the terminal:
SLES12SP1:~ # clear SLES12SP1:~ #
This command exits the current shell:
SLES12SP1:~ # exit logout trainee@SLES12SP1:~>
[stextbox id='black' image='null'] To do : Use the help command with exit option to view the command line switches. [/stextbox]
This command logs out a user from a login shell writing the utmp and wtmp entries in the log files.
[stextbox id='black' image='null'] To do : Use the help command with logout option to view the command line switches. [/stextbox]
This command pauses for a number seconds. The number is specified as the first argument.
[stextbox id='black' image='null'] To do : Use the –help option of the sleep command to view the command line switches. [/stextbox]
wall displays a message, or the contents of a file, or otherwise its standard input, on the terminals of all currently logged in users.The command will wrap lines that are longer than 79 characters. Short lines are whitespace padded to have 79 characters. The command will always put a carriage return and new line at the end of each line. Only the superuser can write on the terminals of users who have chosen to deny messages or are using a program which automatically denies messages. Reading from a file is refused when the invoker is not superuser and the program is suid or sgid.
Start a second session as trainee via ssh on your VM. Return to your first session as root and type :
trainee@SLES12SP1:~> su - Password: fenestros SLES12SP1:~ # wall this is a message from root Broadcast message from trainee@SLES12SP1 (pts/1) (Sat Oct 1 18:39:50 2016): this is a message from root SLES12SP1:~ #
In the second session you should see the following message :
Broadcast message from trainee@SLES12SP1 (pts/1) (Sat Oct 1 18:39:50 2016): this is a message from root
The seq command prints numbers from FIRST to LAST, in steps of INCREMENT:
For example :
SLES12SP1:~ # seq 10 1 2 3 4 5 6 7 8 9 10 SLES12SP1:~ # seq 20 30 20 21 22 23 24 25 26 27 28 29 30 SLES12SP1:~ # seq 20 10 90 20 30 40 50 60 70 80 90 SLES12SP1:~ #
[stextbox id='black' image='null'] To do : Use the –help option of the seq command to view the command line switches. [/stextbox]
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.
SLES12SP1:~ # which screen /usr/bin/screen
Create a session with screen:
SLES12SP1:~ # screen -S mysession
Now press the CTRL and A keys, release the A key and press the C key in order to create a second nested screen.
To return to the first screen, use the CTRL A A keys. This allows you to toggle between the last two screens used.
To see the status of all active screens, use the screen -ls command:
SLES12SP1:~ # screen -ls There is a screen on: 11827.mysession (Attached) 1 Socket in /var/run/screens/S-root.
Now enter the following commands:
SLES12SP1:~ # sleep 9999 & [1] 19879 SLES12SP1:~ # jobs [1]+ Running sleep 9999 & SLES12SP1:~ #
In order to detach the current screen press the CTRL and A keys, release the A key and press the D key:
[detached from 11827.mysession] SLES12SP1:~ #
To re-attach the screen, execute the following command:
SLES12SP1:~ # screen -r
Using the jobs command, check if the process created by the sleep command is still running:unit
SLES12SP1:~ # 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:
[detached from 11827.mysession] SLES12SP1:~ #
Now create a new, non-nested screen:
SLES12SP1:~ # screen -S mysession1
Use the screen -ls command to see what has happened:
SLES12SP1:~ # screen -ls There are screens on: 26655.mysession1 (Attached) 11827.mysession (Detached) 2 Sockets in /var/run/screens/S-root.
To re-attach a specific screen, reference it by it's number:
SLES12SP1:~ # screen -r 11827
Finally, check out what has happened:
SLES12SP1:~ # sleep 9999 & [1] 19879 SLES12SP1:~ # jobs [1]+ Running sleep 9999 & SLES12SP1:~ # jobs [1]+ Running sleep 9999 & SLES12SP1:~ # screen -ls There are screens on: 26655.mysession1 (Attached) 11827.mysession (Attached) 2 Sockets in /var/run/screens/S-root.
[stextbox id='black' image='null'] To do : Use the –help option of the screen command to view the command line switches. [/stextbox]
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:
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:
SLES12SP1:~ # ls -lai /tmp total 8 256 drwxrwxrwt 1 root root 802 Oct 2 01:00 . 256 drwxr-xr-x 1 root root 156 May 1 14:55 .. 258 drwxrwxrwt 1 root root 0 May 3 13:54 .ICE-unix 261 drwxrwxrwt 1 root root 0 May 2 15:56 .Test-unix 257 drwxrwxrwt 1 root root 0 May 3 13:53 .X11-unix 259 drwxrwxrwt 1 root root 0 May 2 15:56 .XIM-unix 373 drwx------ 1 trainee users 12 May 3 13:54 .esd-1000 279 drwx------ 1 gdm gdm 0 May 3 13:54 .esd-486 260 drwxrwxrwt 1 root root 0 May 2 15:56 .font-unix 502 srw------- 1 root root 0 Sep 30 10:26 .java_pid2399 462 srw------- 1 root root 0 Aug 18 12:48 .java_pid2546 448 srw------- 1 root root 0 May 3 14:01 .java_pid3281 403 drwxr-xr-x 1 root root 8 Sep 30 10:26 hsperfdata_root 488 drwxr-xr-x 1 trainee users 48 Aug 18 13:14 inode 444 -rwxrwxrwx 1 root root 50048 May 3 14:01 managera1411267841657715235client 439 -rwxrwxrwx 1 root root 50048 May 3 14:01 managera3336001029897679475server 440 -rwxrwxrwx 1 root root 50048 May 3 14:01 managera4847938942232964844client 443 -rwxrwxrwx 1 root root 50048 May 3 14:01 managera5050357016347721452server 388 drwx------ 1 root root 6 May 3 13:54 systemd-private-04f820fa26c745be8ddba814c6292f21-rtkit-daemon.service-o4lKP5 427 -rw-r--r-- 1 root root 0 May 3 14:01 systemicontmp5578677472245134133dat 426 drwxr-xr-x 1 root root 0 May 3 14:01 systemicontmp7082392205020802884dat SLES12SP1:~ # ls -ali /tmp total 8 256 drwxrwxrwt 1 root root 802 Oct 2 01:00 . 256 drwxr-xr-x 1 root root 156 May 1 14:55 .. 258 drwxrwxrwt 1 root root 0 May 3 13:54 .ICE-unix 261 drwxrwxrwt 1 root root 0 May 2 15:56 .Test-unix 257 drwxrwxrwt 1 root root 0 May 3 13:53 .X11-unix 259 drwxrwxrwt 1 root root 0 May 2 15:56 .XIM-unix 373 drwx------ 1 trainee users 12 May 3 13:54 .esd-1000 279 drwx------ 1 gdm gdm 0 May 3 13:54 .esd-486 260 drwxrwxrwt 1 root root 0 May 2 15:56 .font-unix 502 srw------- 1 root root 0 Sep 30 10:26 .java_pid2399 462 srw------- 1 root root 0 Aug 18 12:48 .java_pid2546 448 srw------- 1 root root 0 May 3 14:01 .java_pid3281 403 drwxr-xr-x 1 root root 8 Sep 30 10:26 hsperfdata_root 488 drwxr-xr-x 1 trainee users 48 Aug 18 13:14 inode 444 -rwxrwxrwx 1 root root 50048 May 3 14:01 managera1411267841657715235client 439 -rwxrwxrwx 1 root root 50048 May 3 14:01 managera3336001029897679475server 440 -rwxrwxrwx 1 root root 50048 May 3 14:01 managera4847938942232964844client 443 -rwxrwxrwx 1 root root 50048 May 3 14:01 managera5050357016347721452server 388 drwx------ 1 root root 6 May 3 13:54 systemd-private-04f820fa26c745be8ddba814c6292f21-rtkit-daemon.service-o4lKP5 427 -rw-r--r-- 1 root root 0 May 3 14:01 systemicontmp5578677472245134133dat 426 drwxr-xr-x 1 root root 0 May 3 14:01 systemicontmp7082392205020802884dat
However ls -l –all –inode cannot be written ls -l –allinode:
SLES12SP1:~ # ls -l --all --inode /tmp total 8 256 drwxrwxrwt 1 root root 802 Oct 2 01:00 . 256 drwxr-xr-x 1 root root 156 May 1 14:55 .. 258 drwxrwxrwt 1 root root 0 May 3 13:54 .ICE-unix 261 drwxrwxrwt 1 root root 0 May 2 15:56 .Test-unix 257 drwxrwxrwt 1 root root 0 May 3 13:53 .X11-unix 259 drwxrwxrwt 1 root root 0 May 2 15:56 .XIM-unix 373 drwx------ 1 trainee users 12 May 3 13:54 .esd-1000 279 drwx------ 1 gdm gdm 0 May 3 13:54 .esd-486 260 drwxrwxrwt 1 root root 0 May 2 15:56 .font-unix 502 srw------- 1 root root 0 Sep 30 10:26 .java_pid2399 462 srw------- 1 root root 0 Aug 18 12:48 .java_pid2546 448 srw------- 1 root root 0 May 3 14:01 .java_pid3281 403 drwxr-xr-x 1 root root 8 Sep 30 10:26 hsperfdata_root 488 drwxr-xr-x 1 trainee users 48 Aug 18 13:14 inode 444 -rwxrwxrwx 1 root root 50048 May 3 14:01 managera1411267841657715235client 439 -rwxrwxrwx 1 root root 50048 May 3 14:01 managera3336001029897679475server 440 -rwxrwxrwx 1 root root 50048 May 3 14:01 managera4847938942232964844client 443 -rwxrwxrwx 1 root root 50048 May 3 14:01 managera5050357016347721452server 388 drwx------ 1 root root 6 May 3 13:54 systemd-private-04f820fa26c745be8ddba814c6292f21-rtkit-daemon.service-o4lKP5 427 -rw-r--r-- 1 root root 0 May 3 14:01 systemicontmp5578677472245134133dat 426 drwxr-xr-x 1 root root 0 May 3 14:01 systemicontmp7082392205020802884dat SLES12SP1:~ # ls -l --allinode /tmp ls: unrecognized option '--allinode' Try 'ls --help' for more information.
[stextbox id='black' image='null'] Important - You should not combine any short options that take an argument. [/stextbox]
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:
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 |
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 … |
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 |
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.
[stextbox id='black' image='null'] To do : Use the –help option of the grep command to view the command line switches. [/stextbox]
The egrep command is identical to the grep command when used with the -E switch. Both can use EREs.
[stextbox id='black' image='null'] To do : Use the –help option of the egrep command to view the command line switches. [/stextbox]
The fgrep command is identical to the grep command when used with the -F switch. Both have no knowledge of Regular Expressions.
[stextbox id='black' image='null'] To do : Use the –help option of the fgrep command to view the command line switches. [/stextbox]
Download the following file by clicking on it's title:
fenestrOS fenestros 555-5555 f .fenestros .fe £
Move the file to the /tmp directory:
SLES12SP1:~ # mv /home/trainee/Downloads/greptest /tmp/greptest
Now use grep to search for lines containing at least one uppercase or lowercase letter:
SLES12SP1:~ # 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:
SLES12SP1:~ # 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:
SLES12SP1:~ # 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:
SLES12SP1:~ # grep '^.$' /tmp/greptest f £
To search for a line containing a special character such as ., that character needs to be preceded by \:
SLES12SP1:~ # grep '^\.' /tmp/greptest .fenestros .fe
[stextbox id='black' image='null'] The grep command can also be used to search for a string in all the files within a specific directory as follows grep -rnw 'directory' -e “pattern”. You can also search only within certain files by specifying the files extensions: grep –include={*.doc,*.xls} -rnw 'directory' -e “pattern”. Finally you can exclude certain file extensions as follows: grep –exclude=*.doc -rnw 'directory' -e “pattern”. [/stextbox]
Download the following file by clicking on it's title:
# Starting comment fenestrOS fenestros # Another comment 555-5555 f .fenestros .fe £ # End comment
Move the file to the /tmp directory:
SLES12SP1:~ # mv /home/trainee/Downloads/greptest /tmp/greptest
Now use the grep command with the -E switch to remove all the comments and empty lines:
SLES12SP1:~ # grep -E -v '^(#|$)' /tmp/greptest fenestrOS fenestros 555-5555 f .fenestros .fe £
[stextbox id='black' image='null'] The expression '^(#|$)' matches all lines beginning with the # character OR all lines with zero characters between the start and the end of the line. [/stextbox]
Now use the egrep command to do the same thing, this time redirecting the output to the file /tmp/greptest1:
SLES12SP1:~ # egrep -v '^(#|$)' /tmp/greptest > /tmp/greptest1 SLES12SP1:~ # cat /tmp/greptest1 fenestrOS fenestros 555-5555 f .fenestros .fe £
[stextbox id='black' image='null'] Important: The above command is very useful when you want to quickly ascertain which directives are active in a very long configuration file. [/stextbox]
Download the following file by clicking on it's title:
# Starting comment ^ This line will be used to demonstrate the use of fgrep fenestrOS fenestros # Another comment 555-5555 f .fenestros .fe £ # End comment
Move the file to the /tmp directory:
SLES12SP1:~ # mv /home/trainee/Downloads/greptest /tmp/greptest
Now use fgrep to match the line starting with the ^ character:
SLES12SP1:~ # 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:
SLES12SP1:~ # 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:
SLES12SP1:~ # grep '^\^' /tmp/greptest ^ This line will be used to demonstrate the use of fgrep
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 |
[stextbox id='black' image='null'] To do : Use the –help option of the fgrep command to view the command line switches. [/stextbox]
Start by displaying the contents of the file /etc/services whilst inhibiting the display of the first 10 lines:
SLES12SP1:~ # sed '1,10d' /etc/services | more # See also: services(5) # # # SERVICE NAME AND TRANSPORT PROTOCOL PORT NUMBER REGISTRY # # (last updated on 2011-11-23) # # ************************************************************************ # * Since we merged and converted this registry it loads more slowly. We * # * know it should load faster and are working on ways to achieve that. * # * Thank you for your patience. * # ************************************************************************ # # Service names and port numbers are used to distinguish between different # services that run over transport protocols such as TCP, UDP, DCCP, and # SCTP. # # Service names are assigned on a first-come, first-served process, as # documented in [RFC6335]. # # Port numbers are assigned in various ways, based on three ranges: System # Ports (0-1023), User Ports (1024-49151), and the Dynamic and/or Private # Ports (49152-65535); the difference uses of these ranges is described in --More--
Now display the same file without any commented lines:
SLES12SP1:~ # sed '/^#/d' /etc/services | more tcpmux 1/tcp # TCP Port Service Multiplexer [Mark_Lottor] tcpmux 1/udp # TCP Port Service Multiplexer [Mark_Lottor] compressnet 2/tcp # Management Utility compressnet 2/udp # Management Utility compressnet 3/tcp # Compression Process [Bernie_Volz] compressnet 3/udp # Compression Process [Bernie_Volz] rje 5/tcp # Remote Job Entry [Jon_Postel] rje 5/udp # Remote Job Entry [Jon_Postel] echo 7/tcp # Echo [Jon_Postel] echo 7/udp # Echo [Jon_Postel] discard 9/tcp # Discard [Jon_Postel] discard 9/udp # Discard [Jon_Postel] discard 9/sctp # Discard [Randall_Stewart] [RFC4960] discard 9/dccp # Discard [Eddie_Kohler] [RFC4340] systat 11/tcp # Active Users [Jon_Postel] systat 11/udp # Active Users [Jon_Postel] daytime 13/tcp # Daytime [RFC867] [Jon_Postel] daytime 13/udp # Daytime [RFC867] [Jon_Postel] qotd 17/tcp # Quote of the Day [Jon_Postel] qotd 17/udp # Quote of the Day [Jon_Postel] msp 18/tcp # Message Send Protocol (historic) [Rina_Nethan iel] msp 18/udp # Message Send Protocol (historic) [Rina_Nethan --More--
[stextbox id='black' image='null'] Important: Note that the BRE is preceeded and followed by the / character. [/stextbox]
Continue by trying to just display the first two lines of /etc/passwd:
SLES12SP1:~ # sed '1,2p' /etc/passwd at:x:25:25:Batch jobs daemon:/var/spool/atjobs:/bin/bash at:x:25:25:Batch jobs daemon:/var/spool/atjobs:/bin/bash bin:x:1:1:bin:/bin:/bin/bash bin:x:1:1:bin:/bin:/bin/bash daemon:x:2:2:Daemon:/sbin:/bin/bash ftp:x:40:49:FTP account:/srv/ftp:/bin/bash ftpsecure:x:488:65534:Secure FTP User:/var/lib/empty:/bin/false ...
[stextbox id='black' image='null'] Important: As you can see in the above output, the command used displays not only the first two lines but also the entire file. As a result the first two lines are displayed twice. [/stextbox]
To force sed to only display the lines you specify, use the -n switch:
SLES12SP1:~ # sed -n '1,2p' /etc/passwd at:x:25:25:Batch jobs daemon:/var/spool/atjobs:/bin/bash bin:x:1:1:bin:/bin:/bin/bash
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:
SLES12SP1:~ # sed -n '/^#/!w /tmp/sedtest' /etc/services SLES12SP1:~ # more /tmp/sedtest tcpmux 1/tcp # TCP Port Service Multiplexer [Mark_Lottor] tcpmux 1/udp # TCP Port Service Multiplexer [Mark_Lottor] compressnet 2/tcp # Management Utility compressnet 2/udp # Management Utility compressnet 3/tcp # Compression Process [Bernie_Volz] compressnet 3/udp # Compression Process [Bernie_Volz] rje 5/tcp # Remote Job Entry [Jon_Postel] rje 5/udp # Remote Job Entry [Jon_Postel] echo 7/tcp # Echo [Jon_Postel] echo 7/udp # Echo [Jon_Postel] discard 9/tcp # Discard [Jon_Postel] discard 9/udp # Discard [Jon_Postel] discard 9/sctp # Discard [Randall_Stewart] [RFC4960] discard 9/dccp # Discard [Eddie_Kohler] [RFC4340] systat 11/tcp # Active Users [Jon_Postel] systat 11/udp # Active Users [Jon_Postel] daytime 13/tcp # Daytime [RFC867] [Jon_Postel] daytime 13/udp # Daytime [RFC867] [Jon_Postel] qotd 17/tcp # Quote of the Day [Jon_Postel] qotd 17/udp # Quote of the Day [Jon_Postel] msp 18/tcp # Message Send Protocol (historic) [Rina_Nethan iel] msp 18/udp # Message Send Protocol (historic) [Rina_Nethan --More--(0%)
[stextbox id='black' image='null'] Important: In the above command, we start by matching all lines in the /etc/services file that start with a #. We then tell sed to write all non-matching lines to the file /tmp/sedtest. [/stextbox]
Finally, create a file containing user1,user2,user3. Replace the commas by spaces:
SLES12SP1:~ # echo "user1,user2,user3" > /tmp/sedtest1 SLES12SP1:~ # cat /tmp/sedtest1 | sed 's/,/ /g' user1 user2 user3
[stextbox id='black' image='null'] Important: The above sed command has the following format s/what is to be replaced (character, string or BRE)/replacement/g. The use of the g character forces sed to replace all occurences that match. If g is not stipulated, only the first matching occurence is replaced. [/stextbox]
The awk command acts as a filter and uses the following syntax:
awk [-F seperator] '[condition] {action}' [file]
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:
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:
SLES12SP1:~ # ls -l | awk '{print $8 $3 $4}' 06:55traineeusers 06:55traineeusers 06:55traineeusers 09:04traineeusers 15:41traineeusers 2016traineeusers 2016traineeusers 14:00traineeusers 15:59traineeusers 16:16traineeusers 16:16traineeusers 17:00traineeusers 16:14traineeusers 2016traineeusers 2016traineeusers 2016traineeusers 2016traineeusers 2016traineeusers 15:46traineeusers 03:59traineeusers 2016traineeusers 10:27traineeusers 06:55traineeusers
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:
SLES12SP1:~ # ls -l | awk '{print $8 " " $3 " " $4}' 06:55 trainee users 06:55 trainee users 06:55 trainee users 09:04 trainee users 15:41 trainee users 2016 trainee users 2016 trainee users 14:00 trainee users 15:59 trainee users 16:16 trainee users 16:16 trainee users 17:00 trainee users 16:14 trainee users 2016 trainee users 2016 trainee users 2016 trainee users 2016 trainee users 2016 trainee users 15:46 trainee users 03:59 trainee users 2016 trainee users 10:27 trainee users 06:55 trainee users
Operators
Operator | Condition |
---|---|
< | Less than |
⇐ | Less than or equal to |
== | Equal to |
!= | Different |
> | Greater than |
>= | Greater than or equal to |
Operators
Operator | Condition |
---|---|
|| | OR |
&& | AND |
! | NO |
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” |
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:
For example:
SLES12SP1:~ # cat > awkscript BEGIN { print "List of the currently mounted file systems"} {print $0} END { print "=========================================="} [^D]
Now apply the awk script to /etc/fstab :
SLES12SP1:~ # awk -f awkscript /etc/fstab List of the currently mounted file systems UUID=9e0e4497-5e47-419a-8ff4-9e2dd7d6a11b swap swap defaults 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 / btrfs defaults 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /boot/grub2/i386-pc btrfs subvol=@/boot/grub2/i386-pc 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /boot/grub2/x86_64-efi btrfs subvol=@/boot/grub2/x86_64-efi 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /home btrfs subvol=@/home 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /opt btrfs subvol=@/opt 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /srv btrfs subvol=@/srv 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /tmp btrfs subvol=@/tmp 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /usr/local btrfs subvol=@/usr/local 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /var/crash btrfs subvol=@/var/crash 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /var/lib/libvirt/images btrfs subvol=@/var/lib/libvirt/images 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /var/lib/mailman btrfs subvol=@/var/lib/mailman 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /var/lib/mariadb btrfs subvol=@/var/lib/mariadb 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /var/lib/mysql btrfs subvol=@/var/lib/mysql 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /var/lib/named btrfs subvol=@/var/lib/named 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /var/lib/pgsql btrfs subvol=@/var/lib/pgsql 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /var/log btrfs subvol=@/var/log 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /var/opt btrfs subvol=@/var/opt 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /var/spool btrfs subvol=@/var/spool 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /var/tmp btrfs subvol=@/var/tmp 0 0 UUID=65337196-2d6b-4c8b-b917-30c3867bf265 /.snapshots btrfs subvol=@/.snapshots 0 0 ==========================================
[stextbox id='black' image='null'] Important: Note the use of the -f switch which instructs awk to use the script. [/stextbox]
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 |
awk can use the following control statements:
if condition { command command ... } else { command command ... }
or:
if condition command else command
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 condition { command command ... }
do { command command ... } while condition
[stextbox id='black' image='null'] To do : Use the –help option of the awk command to view the command line switches. [/stextbox]
Download the file sales.txt by clicking on the title below:
# FenestrOs.com # Annual sales by French department # 83 Desktops§100 Portables§50 Servers§21 Ipads§4 # 06 Desktops§99 Portables§60 Servers§8 Ipads§16 # 13 Desktops§130 Portables§65 Servers§12 Ipads§56
Now download the awk script sales.awk by clicking on the title below:
# BEGIN BEGIN { FS="§" } # TABLE $1 !~ /^#/ && $1 !~ /^$/ { sales[$1]+=$2 } # END END { for (pc in sales) printf("PC Type : %s \t Sales (06+13+83) : %10d\n",pc,sales[pc]); }
This script contains 13 lines. The purpose of this script is to calculate the total number of computers sold in the three French departments from the data present in the sales file:
1 # BEGIN 2 BEGIN { 3 FS="§" 4 } 5 # TABLE 6 $1 !~ /^#/ && $1 !~ /^$/ { 7 sales[$1]+=$2 8 } 9 # END 10 END { 11 for (pc in sales) 12 printf("PC Type : %s \t Sales (06+13+83) : %10d\n",pc,sales[pc]); 13 }
It is important that you understand the key lines in the above script:
Now execute the script and check the output is correct:
SLES12SP1:~ # 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
The expand command converts tabulations in a file to spaces and prints the results to STDOUT. With no file as an argument or with the - character as an argument, the command takes it's input from STDIN.
Download the following file:
un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq un deux trois quatre cinq
Move the file to the /root folder:
SLES12SP1:~ # mv /home/trainee/Downloads/expand.txt /root/expand
Use the cat command to view the contents of the file:
SLES12SP1:~ # 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:
SLES12SP1:~ # cat -vet expand un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$
[stextbox id='black' image='null'] Important : As you can see the tabulations are shown as ^I and the end of each line as a $. [/stextbox]
Now use the expand command to convert the tabulations into spaces and send the result to the expand1 file:
SLES12SP1:~ # expand expand > expand1
View the resulting expand1 file with the cat command and the -vet switches:
SLES12SP1:~ # cat -vet expand1 un deux trois quatre cinq$ un deux trois quatre cinq$ un deux trois quatre cinq$ un deux trois quatre cinq$ un deux trois quatre cinq$ un deux trois quatre cinq$ un deux trois quatre cinq$ un deux trois quatre cinq$ un deux trois quatre cinq$ un deux trois quatre cinq$
[stextbox id='black' image='null'] Important : As you can see, the tabulations have been changed into spaces. [/stextbox]
[stextbox id='black' image='null'] To do : Use the –help option of the expand command to view the command line switches. [/stextbox]
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:
SLES12SP1:~ # 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$ SLES12SP1:~ # unexpand -a expand1 > expand2 SLES12SP1:~ # cat -vet expand2 un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$ un^Ideux^Itrois^Iquatre^Icinq$
[stextbox id='black' image='null'] Important : Note that the spaces have been replaced by tabulations. [/stextbox]
[stextbox id='black' image='null'] To do : Use the –help option of the unexpand command to view the command line switches. [/stextbox]
The cut command 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:
SLES12SP1:~ # cut -c1-7 /etc/passwd at:x:25 bin:x:1 daemon: ftp:x:4 ftpsecu games:x gdm:x:4 lp:x:4: mail:x: man:x:1 message news:x: nobody: nscd:x: ntp:x:7 openslp polkitd postfix pulse:x root:x: rpc:x:4 rtkit:x scard:x sshd:x: statd:x usbmux: uucp:x: vnc:x:4 wwwrun: trainee
In order to select columns 1 to 5, columns 10 to 15 and columns 30 and higher, us the following command:
SLES12SP1:~ # cut -c1-5,10-15,30- /etc/passwd at:x:5:Batc/var/spool/atjobs:/bin/bash bin:x:bin:/ daemo2:2:Dan/bash ftp:x49:FTPftp:/bin/bash ftpse:x:488FTP User:/var/lib/empty:/bin/false games2:100:/var/games:/bin/bash gdm:x:485:Ganager daemon:/var/lib/gdm:/bin/false lp:x:Printi/spool/lpd:/bin/bash mail:12:Mair/spool/clientmqueue:/bin/false man:x62:Maner:/var/cache/man:/bin/bash messas:x:49 D-Bus:/var/run/dbus:/bin/false news:13:Newnews:/bin/bash nobod65534:var/lib/nobody:/bin/bash nscd:6:495:/run/nscd:/sbin/nologin ntp:x492:NTlib/ntp:/bin/false opens:494:2n:/var/lib/empty:/sbin/nologin polki:497:4lkitd:/var/lib/polkit:/sbin/nologin postf:51:51n:/var/spool/postfix:/bin/false pulse90:489emon:/var/lib/pulseaudio:/sbin/nologin root:0:rootsh rpc:x:65534ind:/var/lib/empty:/sbin/nologin rtkit91:490proc:/bin/false scard87:487ader:/var/run/pcscd:/usr/sbin/nologin sshd:8:498:r/lib/sshd:/bin/false statd89:655aemon:/var/lib/nfs:/sbin/nologin usbmu493:65emon:/var/lib/usbmuxd:/sbin/nologin uucp::14:Uny system:/etc/uucp:/bin/bash vnc:x:491:uar/lib/empty:/sbin/nologin wwwru30:8:Whe:/var/lib/wwwrun:/bin/false train:1000:ome/trainee:/bin/bash
In order to select the 2nd, 4th and 6th column, use the following command:
SLES12SP1:~ # cut -d: -f2,4,6 /etc/passwd x:25:/var/spool/atjobs x:1:/bin x:2:/sbin x:49:/srv/ftp x:65534:/var/lib/empty x:100:/var/games x:485:/var/lib/gdm x:7:/var/spool/lpd x:12:/var/spool/clientmqueue x:62:/var/cache/man x:499:/var/run/dbus x:13:/etc/news x:65533:/var/lib/nobody x:495:/run/nscd x:492:/var/lib/ntp x:2:/var/lib/empty x:496:/var/lib/polkit x:51:/var/spool/postfix x:489:/var/lib/pulseaudio x:0:/root x:65534:/var/lib/empty x:490:/proc x:487:/var/run/pcscd x:498:/var/lib/sshd x:65534:/var/lib/nfs x:65534:/var/lib/usbmuxd x:14:/etc/uucp x:491:/var/lib/empty x:8:/var/lib/wwwrun x:100:/home/trainee
[stextbox id='black' image='null'] Important: Note the use of the -d switch to change the default seperator. [/stextbox]
[stextbox id='black' image='null'] To do : Use the –help option of the cut command to view the command line switches. [/stextbox]
The following command is used to extract the Primary Group GIDs from the /etc/passwd file:
SLES12SP1:~ # cut -d: -f4 /etc/passwd | sort -n | uniq 0 1 2 7 8 12 13 14 25 49 51 62 100 485 487 489 490 491 492 495 496 498 499 65533 65534
[stextbox id='black' image='null'] Important: Note the use of the uniq command to remove duplicates from the list. [/stextbox]
[stextbox id='black' image='null'] To do : Use the –help option of the uniq command to view the command line switches. [/stextbox]
The tr command is used to substitute certain characters by other characters. This command only accepts data from standard input (hence the pipe):
SLES12SP1:~ # cat /etc/passwd | tr "[a-z]" "[A-Z]" AT:X:25:25:BATCH JOBS DAEMON:/VAR/SPOOL/ATJOBS:/BIN/BASH BIN:X:1:1:BIN:/BIN:/BIN/BASH DAEMON:X:2:2:DAEMON:/SBIN:/BIN/BASH FTP:X:40:49:FTP ACCOUNT:/SRV/FTP:/BIN/BASH FTPSECURE:X:488:65534:SECURE FTP USER:/VAR/LIB/EMPTY:/BIN/FALSE GAMES:X:12:100:GAMES ACCOUNT:/VAR/GAMES:/BIN/BASH GDM:X:486:485:GNOME DISPLAY MANAGER DAEMON:/VAR/LIB/GDM:/BIN/FALSE LP:X:4:7:PRINTING DAEMON:/VAR/SPOOL/LPD:/BIN/BASH MAIL:X:8:12:MAILER DAEMON:/VAR/SPOOL/CLIENTMQUEUE:/BIN/FALSE MAN:X:13:62:MANUAL PAGES VIEWER:/VAR/CACHE/MAN:/BIN/BASH MESSAGEBUS:X:499:499:USER FOR D-BUS:/VAR/RUN/DBUS:/BIN/FALSE NEWS:X:9:13:NEWS SYSTEM:/ETC/NEWS:/BIN/BASH NOBODY:X:65534:65533:NOBODY:/VAR/LIB/NOBODY:/BIN/BASH NSCD:X:496:495:USER FOR NSCD:/RUN/NSCD:/SBIN/NOLOGIN NTP:X:74:492:NTP DAEMON:/VAR/LIB/NTP:/BIN/FALSE OPENSLP:X:494:2:OPENSLP DAEMON:/VAR/LIB/EMPTY:/SBIN/NOLOGIN POLKITD:X:497:496:USER FOR POLKITD:/VAR/LIB/POLKIT:/SBIN/NOLOGIN POSTFIX:X:51:51:POSTFIX DAEMON:/VAR/SPOOL/POSTFIX:/BIN/FALSE PULSE:X:490:489:PULSEAUDIO DAEMON:/VAR/LIB/PULSEAUDIO:/SBIN/NOLOGIN ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH RPC:X:495:65534:USER FOR RPCBIND:/VAR/LIB/EMPTY:/SBIN/NOLOGIN RTKIT:X:491:490:REALTIMEKIT:/PROC:/BIN/FALSE SCARD:X:487:487:SMART CARD READER:/VAR/RUN/PCSCD:/USR/SBIN/NOLOGIN SSHD:X:498:498:SSH DAEMON:/VAR/LIB/SSHD:/BIN/FALSE STATD:X:489:65534:NFS STATD DAEMON:/VAR/LIB/NFS:/SBIN/NOLOGIN USBMUX:X:493:65534:USBMUXD DAEMON:/VAR/LIB/USBMUXD:/SBIN/NOLOGIN UUCP:X:10:14:UNIX-TO-UNIX COPY SYSTEM:/ETC/UUCP:/BIN/BASH VNC:X:492:491:USER FOR VNC:/VAR/LIB/EMPTY:/SBIN/NOLOGIN WWWRUN:X:30:8:WWW DAEMON APACHE:/VAR/LIB/WWWRUN:/BIN/FALSE TRAINEE:X:1000:100:TRAINEE:/HOME/TRAINEE:/BIN/BASH
[stextbox id='black' image='null'] To do : Use the –help option of the tr command to view the command line switches. [/stextbox]
The paste command concatenates lines from n files. For example:
SLES12SP1:~ # paste -d: /etc/passwd /etc/shadow at:x:25:25:Batch jobs daemon:/var/spool/atjobs:/bin/bash:at:!:16922:::::: bin:x:1:1:bin:/bin:/bin/bash:bin:*:16765:::::: daemon:x:2:2:Daemon:/sbin:/bin/bash:daemon:*:16765:::::: ftp:x:40:49:FTP account:/srv/ftp:/bin/bash:ftp:*:16765:::::: ftpsecure:x:488:65534:Secure FTP User:/var/lib/empty:/bin/false:ftpsecure:!:16922:::::: games:x:12:100:Games account:/var/games:/bin/bash:games:*:16765:::::: gdm:x:486:485:Gnome Display Manager daemon:/var/lib/gdm:/bin/false:gdm:!:16922:::::: lp:x:4:7:Printing daemon:/var/spool/lpd:/bin/bash:lp:*:16765:::::: mail:x:8:12:Mailer daemon:/var/spool/clientmqueue:/bin/false:mail:*:16765:::::: man:x:13:62:Manual pages viewer:/var/cache/man:/bin/bash:man:*:16765:::::: messagebus:x:499:499:User for D-Bus:/var/run/dbus:/bin/false:messagebus:!:16765:::::: news:x:9:13:News system:/etc/news:/bin/bash:news:*:16765:::::: nobody:x:65534:65533:nobody:/var/lib/nobody:/bin/bash:nobody:*:16765:::::: nscd:x:496:495:User for nscd:/run/nscd:/sbin/nologin:nscd:!:16765:::::: ntp:x:74:492:NTP daemon:/var/lib/ntp:/bin/false:ntp:!:16922:::::: openslp:x:494:2:openslp daemon:/var/lib/empty:/sbin/nologin:openslp:!:16765:::::: polkitd:x:497:496:User for polkitd:/var/lib/polkit:/sbin/nologin:polkitd:!:16765:::::: postfix:x:51:51:Postfix Daemon:/var/spool/postfix:/bin/false:postfix:!:16922:::::: pulse:x:490:489:PulseAudio daemon:/var/lib/pulseaudio:/sbin/nologin:pulse:!:16922:::::: root:x:0:0:root:/root:/bin/bash:root:$6$g0tHJ9vyIfFt$rbm.rf7p6XZMxMqbqa/BGDeA7E7RkC9n89w8cWdpAxkUmwk7BPcMv7Zy9nVAn7f/7zQJzcRcsIqp5bRx1e8iX/:16922:::::: rpc:x:495:65534:user for rpcbind:/var/lib/empty:/sbin/nologin:rpc:!:16765:::::: rtkit:x:491:490:RealtimeKit:/proc:/bin/false:rtkit:!:16922:::::: scard:x:487:487:Smart Card Reader:/var/run/pcscd:/usr/sbin/nologin:scard:!:16922:::::: sshd:x:498:498:SSH daemon:/var/lib/sshd:/bin/false:sshd:!:16765:::::: statd:x:489:65534:NFS statd daemon:/var/lib/nfs:/sbin/nologin:statd:!:16922:::::: usbmux:x:493:65534:usbmuxd daemon:/var/lib/usbmuxd:/sbin/nologin:usbmux:!:16922:::::: uucp:x:10:14:Unix-to-Unix CoPy system:/etc/uucp:/bin/bash:uucp:*:16765:::::: vnc:x:492:491:user for VNC:/var/lib/empty:/sbin/nologin:vnc:!:16922:::::: wwwrun:x:30:8:WWW daemon apache:/var/lib/wwwrun:/bin/false:wwwrun:*:16765:::::: trainee:x:1000:100:trainee:/home/trainee:/bin/bash:trainee:$6$OZyVqj4ekgmu$Cw0T.n6gNv.vTdAT6dFxrrSeHW/V3r43jWFczPG0lxg5SB9iMUcQ6MFLz9NuTTas289xe/ULsJhE2HdJbraGA.:16922:0:99999:7:::
[stextbox id='black' image='null'] Important: Note that you need to become root to be able to execute this command. The reason is that normal users do not have the right to read the /etc/shadow file. [/stextbox]
[stextbox id='black' image='null'] To do : Use the –help option of the paste command to view the command line switches. [/stextbox]
The split command is used to divide a large file into smaller segments. Create an empty 250 MB file as follows:
SLES12SP1:~ # dd if=/dev/zero of=/file bs=1024k count=250 250+0 records in 250+0 records out 262144000 bytes (262 MB) copied, 3.50609 s, 74.8 MB/s
Now use the split command to divide the file into 5 smaller files each of 50:
SLES12SP1:~ # split -b 50m /file filepart SLES12SP1:~ # ls -l | grep filepart -rw-r--r-- 1 root root 52428800 Dec 14 15:10 filepartaa -rw-r--r-- 1 root root 52428800 Dec 14 15:10 filepartab -rw-r--r-- 1 root root 52428800 Dec 14 15:10 filepartac -rw-r--r-- 1 root root 52428800 Dec 14 15:10 filepartad -rw-r--r-- 1 root root 52428800 Dec 14 15:10 filepartae
[stextbox id='black' image='null'] Important: Note that the 5 files were created in the current working directory. [/stextbox]
You can re-construct the original file by using the cat command:
SLES12SP1:~ # cat fileparta* > newfile SLES12SP1:~ # ls -l | grep newf -rw-r--r-- 1 root root 262144000 Dec 14 15:11 newfile
[stextbox id='black' image='null'] To do : Use the –help option of the split command to view the command line switches. [/stextbox]
The diff command 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:
SLES12SP1:~ # cp /etc/passwd /root
Edit the */root/passwd file as shown:
... trainee10:x:1000:1000:trainee:/home/trainee:/bin/bash ...
Delete the uucp entry and add the following line to the end of /root/passwd:
... Linux is great!
Now compare the two files:
SLES12SP1:~ # diff /etc/passwd /root/passwd 27d26 < uucp:x:10:14:Unix-to-Unix CoPy system:/etc/uucp:/bin/bash 30c29,30 < trainee:x:1000:100:trainee:/home/trainee:/bin/bash --- > trainee10:x:1000:100:trainee:/home/trainee:/bin/bash > 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 27d26 means that line 27 needs to be deleted form /etc/passwd because it does not exist in /root/passwd.
The output 30c29,30 means that line 30 needs to be changed in /etc/passwd so that is the same as line 29 in /root/passwd.
[stextbox id='black' image='null'] To do : Use the –help option of the diff command to view the command line switches. [/stextbox]
The cmp command compares two files character by character. By default, the command stops after finding the first difference:
SLES12SP1:~ # cmp /root/passwd /etc/passwd /root/passwd /etc/passwd differ: char 1413, line 27
The -l switch shows all of the differences in a three column format:
SLES12SP1:~ # cmp -l /root/passwd /etc/passwd | more cmp: EOF on /root/passwd 1413 166 165 1414 156 165 1416 72 160 1417 170 72 1418 72 170 1419 64 72 1420 71 61 1421 62 60 1423 64 61 1424 71 64 1425 61 72 1426 72 125 1427 165 156 1428 163 151 1429 145 170 1430 162 55 1431 40 164 1432 146 157 1433 157 55 1434 162 125 1435 40 156 1436 126 151 1437 116 170 --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.
[stextbox id='black' image='null'] To do : Use the –help option of the cmp command to view the command line switches. [/stextbox]
The patch command is used to apply modifications contained within a patch file to an older version of a file so that it becomes the newer version of the file.
If you recall, you made some changes to the original greptest file that you downloaded:
SLES12SP1:~ # 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:
SLES12SP1:~ # 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:
SLES12SP1:~ # cd /tmp SLES12SP1:/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:
SLES12SP1:/tmp # cat /tmp/greptest.patch --- greptest 2016-12-14 14:00:32.987471124 +0100 +++ greptest1 2016-12-14 13:59:12.319471124 +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:
SLES12SP1:/tmp # patch < greptest.patch patching file greptest
Finally, check the contents of the patched greptest file:
SLES12SP1:/tmp # cat greptest fenestrOS fenestros 555-5555 f .fenestros .fe £
[stextbox id='black' image='null'] To do : Use the –help option of the patch command to view the command line switches. [/stextbox]
The strings Command 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:
Used as is, the command extracts all strings greater than 4 characters in length:
SLES12SP1:/tmp # strings /usr/bin/passwd | more /lib64/ld-linux-x86-64.so.2 cHc` libpam.so.0 _ITM_deregisterTMCloneTable __gmon_start__ _Jv_RegisterClasses _ITM_registerTMCloneTable pam_start pam_strerror pam_chauthtok pam_end libpam_misc.so.0 misc_conv libselinux.so.1 _init is_selinux_enabled security_getenforce context_user_get security_compute_av matchpathcon freecon context_free setfscreatecon --More--
Print the offset within the file before each string:
SLES12SP1:/tmp # strings -t d /usr/bin/passwd | more 568 /lib64/ld-linux-x86-64.so.2 650 cHc` 4977 libpam.so.0 4989 _ITM_deregisterTMCloneTable 5017 __gmon_start__ 5032 _Jv_RegisterClasses 5052 _ITM_registerTMCloneTable 5078 pam_start 5088 pam_strerror 5101 pam_chauthtok 5115 pam_end 5123 libpam_misc.so.0 5140 misc_conv 5150 libselinux.so.1 5166 _init 5172 is_selinux_enabled 5191 security_getenforce 5211 context_user_get 5228 security_compute_av 5248 matchpathcon 5261 freecon 5269 context_free 5282 setfscreatecon --More--
The -t switch can take one of three arguments that specify the numbering system to use:
Argument | Numbering System |
---|---|
d | Decimal |
o | Octal |
x | Hexadecimal |
The -n switch prints sequences of characters that are at least min-len characters long, instead of the default 4:
SLES12SP1:/tmp # strings -t d -n 15 /usr/bin/passwd | more 568 /lib64/ld-linux-x86-64.so.2 4989 _ITM_deregisterTMCloneTable 5032 _Jv_RegisterClasses 5052 _ITM_registerTMCloneTable 5123 libpam_misc.so.0 5150 libselinux.so.1 5172 is_selinux_enabled 5191 security_getenforce 5211 context_user_get 5228 security_compute_av 5510 __stack_chk_fail 5671 __errno_location 6106 __libc_start_main 6202 LIBPAM_MISC_1.0 34104 Usage: %s [options] [LOGIN] 34144 -a, --all report password status on all accounts 34216 -d, --delete delete the password for the named account 34296 -e, --expire force expire the password for the named account 34384 -h, --help display this help message and exit 34456 -k, --keep-tokens change password only if expired 34528 -i, --inactive INACTIVE set password inactive after expiration --More--
The -f switch prints the name of the file before each string:
SLES12SP1:/tmp # strings -f /bin/* | grep "(c)" /bin/btrace: # Copyright (c) 2005 Silicon Graphics, Inc. /bin/csh: @(#) Copyright (c) 1991 The Regents of the University of California. /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/tcsh: @(#) Copyright (c) 1991 The Regents of the University of California.
[stextbox id='black' image='null'] To do : Use the –help option of the strings command to view the command line switches. [/stextbox]
This command compares two text files and prints the differences to standard output:
SLES12SP1:/tmp # comm /etc/passwd /root/passwd at:x:25:25:Batch jobs daemon:/var/spool/atjobs:/bin/bash bin:x:1:1:bin:/bin:/bin/bash daemon:x:2:2:Daemon:/sbin:/bin/bash ftp:x:40:49:FTP account:/srv/ftp:/bin/bash ftpsecure:x:488:65534:Secure FTP User:/var/lib/empty:/bin/false games:x:12:100:Games account:/var/games:/bin/bash gdm:x:486:485:Gnome Display Manager daemon:/var/lib/gdm:/bin/false lp:x:4:7:Printing daemon:/var/spool/lpd:/bin/bash mail:x:8:12:Mailer daemon:/var/spool/clientmqueue:/bin/false man:x:13:62:Manual pages viewer:/var/cache/man:/bin/bash messagebus:x:499:499:User for D-Bus:/var/run/dbus:/bin/false news:x:9:13:News system:/etc/news:/bin/bash nobody:x:65534:65533:nobody:/var/lib/nobody:/bin/bash nscd:x:496:495:User for nscd:/run/nscd:/sbin/nologin ntp:x:74:492:NTP daemon:/var/lib/ntp:/bin/false openslp:x:494:2:openslp daemon:/var/lib/empty:/sbin/nologin polkitd:x:497:496:User for polkitd:/var/lib/polkit:/sbin/nologin postfix:x:51:51:Postfix Daemon:/var/spool/postfix:/bin/false pulse:x:490:489:PulseAudio daemon:/var/lib/pulseaudio:/sbin/nologin root:x:0:0:root:/root:/bin/bash rpc:x:495:65534:user for rpcbind:/var/lib/empty:/sbin/nologin rtkit:x:491:490:RealtimeKit:/proc:/bin/false scard:x:487:487:Smart Card Reader:/var/run/pcscd:/usr/sbin/nologin sshd:x:498:498:SSH daemon:/var/lib/sshd:/bin/false statd:x:489:65534:NFS statd daemon:/var/lib/nfs:/sbin/nologin usbmux:x:493:65534:usbmuxd daemon:/var/lib/usbmuxd:/sbin/nologin uucp:x:10:14:Unix-to-Unix CoPy system:/etc/uucp:/bin/bash vnc:x:492:491:user for VNC:/var/lib/empty:/sbin/nologin wwwrun:x:30:8:WWW daemon apache:/var/lib/wwwrun:/bin/false comm: file 1 is not in sorted order comm: file 2 is not in sorted order trainee10:x:1000:100:trainee:/home/trainee:/bin/bash Linux est super trainee:x:1000:100:trainee:/home/trainee:/bin/bash
[stextbox id='black' image='null'] Important: The lines to the left are those that only appear in the first file. The lines on the right are those that exist in both files. The lines in the middle are those that only exist in the second file. [/stextbox]
If you only want to see the lines common to both files, use the following command:
SLES12SP1:/tmp # comm -12 /etc/passwd /root/passwd at:x:25:25:Batch jobs daemon:/var/spool/atjobs:/bin/bash bin:x:1:1:bin:/bin:/bin/bash daemon:x:2:2:Daemon:/sbin:/bin/bash ftp:x:40:49:FTP account:/srv/ftp:/bin/bash ftpsecure:x:488:65534:Secure FTP User:/var/lib/empty:/bin/false games:x:12:100:Games account:/var/games:/bin/bash gdm:x:486:485:Gnome Display Manager daemon:/var/lib/gdm:/bin/false lp:x:4:7:Printing daemon:/var/spool/lpd:/bin/bash mail:x:8:12:Mailer daemon:/var/spool/clientmqueue:/bin/false man:x:13:62:Manual pages viewer:/var/cache/man:/bin/bash messagebus:x:499:499:User for D-Bus:/var/run/dbus:/bin/false news:x:9:13:News system:/etc/news:/bin/bash nobody:x:65534:65533:nobody:/var/lib/nobody:/bin/bash nscd:x:496:495:User for nscd:/run/nscd:/sbin/nologin ntp:x:74:492:NTP daemon:/var/lib/ntp:/bin/false openslp:x:494:2:openslp daemon:/var/lib/empty:/sbin/nologin polkitd:x:497:496:User for polkitd:/var/lib/polkit:/sbin/nologin postfix:x:51:51:Postfix Daemon:/var/spool/postfix:/bin/false pulse:x:490:489:PulseAudio daemon:/var/lib/pulseaudio:/sbin/nologin root:x:0:0:root:/root:/bin/bash rpc:x:495:65534:user for rpcbind:/var/lib/empty:/sbin/nologin rtkit:x:491:490:RealtimeKit:/proc:/bin/false scard:x:487:487:Smart Card Reader:/var/run/pcscd:/usr/sbin/nologin sshd:x:498:498:SSH daemon:/var/lib/sshd:/bin/false statd:x:489:65534:NFS statd daemon:/var/lib/nfs:/sbin/nologin usbmux:x:493:65534:usbmuxd daemon:/var/lib/usbmuxd:/sbin/nologin vnc:x:492:491:user for VNC:/var/lib/empty:/sbin/nologin wwwrun:x:30:8:WWW daemon apache:/var/lib/wwwrun:/bin/false comm: file 1 is not in sorted order comm: file 2 is not in sorted order
[stextbox id='black' image='null'] To do : Use the –help option of the comm command to view the command line switches. [/stextbox]
The head command is used to display the first x lines of a file. The default value of x is 10:
SLES12SP1:/tmp # head /etc/passwd at:x:25:25:Batch jobs daemon:/var/spool/atjobs:/bin/bash bin:x:1:1:bin:/bin:/bin/bash daemon:x:2:2:Daemon:/sbin:/bin/bash ftp:x:40:49:FTP account:/srv/ftp:/bin/bash ftpsecure:x:488:65534:Secure FTP User:/var/lib/empty:/bin/false games:x:12:100:Games account:/var/games:/bin/bash gdm:x:486:485:Gnome Display Manager daemon:/var/lib/gdm:/bin/false lp:x:4:7:Printing daemon:/var/spool/lpd:/bin/bash mail:x:8:12:Mailer daemon:/var/spool/clientmqueue:/bin/false man:x:13:62:Manual pages viewer:/var/cache/man:/bin/bash
You can change the default value of x by using the -n switch:
SLES12SP1:/tmp # head -n 15 /etc/passwd at:x:25:25:Batch jobs daemon:/var/spool/atjobs:/bin/bash bin:x:1:1:bin:/bin:/bin/bash daemon:x:2:2:Daemon:/sbin:/bin/bash ftp:x:40:49:FTP account:/srv/ftp:/bin/bash ftpsecure:x:488:65534:Secure FTP User:/var/lib/empty:/bin/false games:x:12:100:Games account:/var/games:/bin/bash gdm:x:486:485:Gnome Display Manager daemon:/var/lib/gdm:/bin/false lp:x:4:7:Printing daemon:/var/spool/lpd:/bin/bash mail:x:8:12:Mailer daemon:/var/spool/clientmqueue:/bin/false man:x:13:62:Manual pages viewer:/var/cache/man:/bin/bash messagebus:x:499:499:User for D-Bus:/var/run/dbus:/bin/false news:x:9:13:News system:/etc/news:/bin/bash nobody:x:65534:65533:nobody:/var/lib/nobody:/bin/bash nscd:x:496:495:User for nscd:/run/nscd:/sbin/nologin ntp:x:74:492:NTP daemon:/var/lib/ntp:/bin/false
The command can also be used to display the first y bytes of a file by using the -c switch:
SLES12SP1:/tmp # head -c 150 /etc/passwd at:x:25:25:Batch jobs daemon:/var/spool/atjobs:/bin/bash bin:x:1:1:bin:/bin:/bin/bash daemon:x:2:2:Daemon:/sbin:/bin/bash ftp:x:40:49:FTP account:/srvSLES12SP1:/tmp #
If the value of y is negative, head displays all bytes in the file except the last y bytes:
SLES12SP1:/tmp # head -c -150 /etc/passwd at:x:25:25:Batch jobs daemon:/var/spool/atjobs:/bin/bash bin:x:1:1:bin:/bin:/bin/bash daemon:x:2:2:Daemon:/sbin:/bin/bash ftp:x:40:49:FTP account:/srv/ftp:/bin/bash ftpsecure:x:488:65534:Secure FTP User:/var/lib/empty:/bin/false games:x:12:100:Games account:/var/games:/bin/bash gdm:x:486:485:Gnome Display Manager daemon:/var/lib/gdm:/bin/false lp:x:4:7:Printing daemon:/var/spool/lpd:/bin/bash mail:x:8:12:Mailer daemon:/var/spool/clientmqueue:/bin/false man:x:13:62:Manual pages viewer:/var/cache/man:/bin/bash messagebus:x:499:499:User for D-Bus:/var/run/dbus:/bin/false news:x:9:13:News system:/etc/news:/bin/bash nobody:x:65534:65533:nobody:/var/lib/nobody:/bin/bash nscd:x:496:495:User for nscd:/run/nscd:/sbin/nologin ntp:x:74:492:NTP daemon:/var/lib/ntp:/bin/false openslp:x:494:2:openslp daemon:/var/lib/empty:/sbin/nologin polkitd:x:497:496:User for polkitd:/var/lib/polkit:/sbin/nologin postfix:x:51:51:Postfix Daemon:/var/spool/postfix:/bin/false pulse:x:490:489:PulseAudio daemon:/var/lib/pulseaudio:/sbin/nologin root:x:0:0:root:/root:/bin/bash rpc:x:495:65534:user for rpcbind:/var/lib/empty:/sbin/nologin rtkit:x:491:490:RealtimeKit:/proc:/bin/false scard:x:487:487:Smart Card Reader:/var/run/pcscd:/usr/sbin/nologin sshd:x:498:498:SSH daemon:/var/lib/sshd:/bin/false statd:x:489:65534:NFS statd daemon:/var/lib/nfs:/sbin/nologin usbmux:x:493:65534:usbmuxd daemon:/var/lib/usbmuxd:/sbin/nologin uucp:x:10:14:Unix-to-Unix CoPy system:/etc/uucp:/bin/bash vnc:x:492:491:usSLES12SP1:/tmp #
Both x and y can accept multipliers:
SLES12SP1:/tmp # head -c 1b /etc/passwd at:x:25:25:Batch jobs daemon:/var/spool/atjobs:/bin/bash bin:x:1:1:bin:/bin:/bin/bash daemon:x:2:2:Daemon:/sbin:/bin/bash ftp:x:40:49:FTP account:/srv/ftp:/bin/bash ftpsecure:x:488:65534:Secure FTP User:/var/lib/empty:/bin/false games:x:12:100:Games account:/var/games:/bin/bash gdm:x:486:485:Gnome Display Manager daemon:/var/lib/gdm:/bin/false lp:x:4:7:Printing daemon:/var/spool/lpd:/bin/bash mail:x:8:12:Mailer daemon:/var/spool/clientmqueue:/bin/false man:x:13:62:Manual pages viewer:/var/cache/man:/bin/basSLES12SP1:/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 |
[stextbox id='black' image='null'] To do : Use the –help option of the head command to view the command line switches. [/stextbox]
The tail command is used to display the last x lines of a file. The default value of x is 10:
SLES12SP1:/tmp # tail /etc/passwd rpc:x:495:65534:user for rpcbind:/var/lib/empty:/sbin/nologin rtkit:x:491:490:RealtimeKit:/proc:/bin/false scard:x:487:487:Smart Card Reader:/var/run/pcscd:/usr/sbin/nologin sshd:x:498:498:SSH daemon:/var/lib/sshd:/bin/false statd:x:489:65534:NFS statd daemon:/var/lib/nfs:/sbin/nologin usbmux:x:493:65534:usbmuxd daemon:/var/lib/usbmuxd:/sbin/nologin uucp:x:10:14:Unix-to-Unix CoPy system:/etc/uucp:/bin/bash vnc:x:492:491:user for VNC:/var/lib/empty:/sbin/nologin wwwrun:x:30:8:WWW daemon apache:/var/lib/wwwrun:/bin/false trainee:x:1000:100:trainee:/home/trainee:/bin/bash
You can change the default value of x by using the -n switch:
SLES12SP1:/tmp # tail -n 15 /etc/passwd openslp:x:494:2:openslp daemon:/var/lib/empty:/sbin/nologin polkitd:x:497:496:User for polkitd:/var/lib/polkit:/sbin/nologin postfix:x:51:51:Postfix Daemon:/var/spool/postfix:/bin/false pulse:x:490:489:PulseAudio daemon:/var/lib/pulseaudio:/sbin/nologin root:x:0:0:root:/root:/bin/bash rpc:x:495:65534:user for rpcbind:/var/lib/empty:/sbin/nologin rtkit:x:491:490:RealtimeKit:/proc:/bin/false scard:x:487:487:Smart Card Reader:/var/run/pcscd:/usr/sbin/nologin sshd:x:498:498:SSH daemon:/var/lib/sshd:/bin/false statd:x:489:65534:NFS statd daemon:/var/lib/nfs:/sbin/nologin usbmux:x:493:65534:usbmuxd daemon:/var/lib/usbmuxd:/sbin/nologin uucp:x:10:14:Unix-to-Unix CoPy system:/etc/uucp:/bin/bash vnc:x:492:491:user for VNC:/var/lib/empty:/sbin/nologin wwwrun:x:30:8:WWW daemon apache:/var/lib/wwwrun:/bin/false trainee:x:1000:100:trainee:/home/trainee:/bin/bash
The command can also be used to display the last y bytes of a file by using the -c switch:
SLES12SP1:/tmp # tail -c 150 /etc/passwd er for VNC:/var/lib/empty:/sbin/nologin wwwrun:x:30:8:WWW daemon apache:/var/lib/wwwrun:/bin/false trainee:x:1000:100:trainee:/home/trainee:/bin/bash
If the value of y is positive, tail displays all bytes in the file after the yth byte:
SLES12SP1:/tmp # tail -c 150 /etc/passwd er for VNC:/var/lib/empty:/sbin/nologin wwwrun:x:30:8:WWW daemon apache:/var/lib/wwwrun:/bin/false trainee:x:1000:100:trainee:/home/trainee:/bin/bash SLES12SP1:/tmp # tail -c +150 /etc/passwd v/ftp:/bin/bash ftpsecure:x:488:65534:Secure FTP User:/var/lib/empty:/bin/false games:x:12:100:Games account:/var/games:/bin/bash gdm:x:486:485:Gnome Display Manager daemon:/var/lib/gdm:/bin/false lp:x:4:7:Printing daemon:/var/spool/lpd:/bin/bash mail:x:8:12:Mailer daemon:/var/spool/clientmqueue:/bin/false man:x:13:62:Manual pages viewer:/var/cache/man:/bin/bash messagebus:x:499:499:User for D-Bus:/var/run/dbus:/bin/false news:x:9:13:News system:/etc/news:/bin/bash nobody:x:65534:65533:nobody:/var/lib/nobody:/bin/bash nscd:x:496:495:User for nscd:/run/nscd:/sbin/nologin ntp:x:74:492:NTP daemon:/var/lib/ntp:/bin/false openslp:x:494:2:openslp daemon:/var/lib/empty:/sbin/nologin polkitd:x:497:496:User for polkitd:/var/lib/polkit:/sbin/nologin postfix:x:51:51:Postfix Daemon:/var/spool/postfix:/bin/false pulse:x:490:489:PulseAudio daemon:/var/lib/pulseaudio:/sbin/nologin root:x:0:0:root:/root:/bin/bash rpc:x:495:65534:user for rpcbind:/var/lib/empty:/sbin/nologin rtkit:x:491:490:RealtimeKit:/proc:/bin/false scard:x:487:487:Smart Card Reader:/var/run/pcscd:/usr/sbin/nologin sshd:x:498:498:SSH daemon:/var/lib/sshd:/bin/false statd:x:489:65534:NFS statd daemon:/var/lib/nfs:/sbin/nologin usbmux:x:493:65534:usbmuxd daemon:/var/lib/usbmuxd:/sbin/nologin uucp:x:10:14:Unix-to-Unix CoPy system:/etc/uucp:/bin/bash vnc:x:492:491:user for VNC:/var/lib/empty:/sbin/nologin wwwrun:x:30:8:WWW daemon apache:/var/lib/wwwrun:/bin/false trainee:x:1000:100:trainee:/home/trainee:/bin/bash
Both x and y can accept multipliers:
SLES12SP1:/tmp # tail -c 1b /etc/passwd kit:x:491:490:RealtimeKit:/proc:/bin/false scard:x:487:487:Smart Card Reader:/var/run/pcscd:/usr/sbin/nologin sshd:x:498:498:SSH daemon:/var/lib/sshd:/bin/false statd:x:489:65534:NFS statd daemon:/var/lib/nfs:/sbin/nologin usbmux:x:493:65534:usbmuxd daemon:/var/lib/usbmuxd:/sbin/nologin uucp:x:10:14:Unix-to-Unix CoPy system:/etc/uucp:/bin/bash vnc:x:492:491:user for VNC:/var/lib/empty:/sbin/nologin wwwrun:x:30:8:WWW daemon apache:/var/lib/wwwrun:/bin/false trainee:x:1000:100:trainee:/home/trainee:/bin/bash
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:
SLES12SP1:/tmp # tail -f /var/log/messages 2016-12-14T20:45:01.238708+01:00 SLES12SP1 systemd: pam_unix(systemd-user:session): session opened for user root by (uid=0) 2016-12-14T20:45:01.275146+01:00 SLES12SP1 CRON[17392]: pam_unix(crond:session): session closed for user root 2016-12-14T20:45:01.288315+01:00 SLES12SP1 systemd: pam_unix(systemd-user:session): session closed for user root 2016-12-14T20:45:41.689270+01:00 SLES12SP1 sh[1320]: Sleeping '' '' 2016-12-14T20:58:07.757988+01:00 SLES12SP1 sh[1320]: message repeated 4 times: [ Sleeping '' ''] 2016-12-14T21:00:01.284105+01:00 SLES12SP1 cron[23239]: pam_unix(crond:session): session opened for user root by (uid=0) 2016-12-14T21:00:01.291722+01:00 SLES12SP1 systemd: pam_unix(systemd-user:session): session opened for user root by (uid=0) 2016-12-14T21:00:01.338305+01:00 SLES12SP1 CRON[23239]: pam_unix(crond:session): session closed for user root 2016-12-14T21:00:01.351426+01:00 SLES12SP1 systemd: pam_unix(systemd-user:session): session closed for user root 2016-12-14T21:01:14.285113+01:00 SLES12SP1 sh[1320]: Sleeping '' '' ^C
[stextbox id='black' image='null'] To do : Use the –help option of the head command to view the command line switches. [/stextbox]
SLES12SP1:/tmp # ifconfig eth0 eth0 Link encap:Ethernet HWaddr 08:00:27:10:B5:86 inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe10:b586/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:8226 errors:0 dropped:0 overruns:0 frame:0 TX packets:16490 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:588832 (575.0 Kb) TX bytes:1303594 (1.2 Mb) SLES12SP1:/tmp # ifconfig eth0 | grep "inet" inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe10:b586/64 Scope:Link SLES12SP1:/tmp # ifconfig eth0 | grep "inet" | grep -v "inet6" inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0 SLES12SP1:/tmp # ifconfig eth0 | grep "inet" | grep -v "inet6" | tr -s " " ":" :inet:addr:10.0.2.15:Bcast:10.0.2.255:Mask:255.255.255.0 SLES12SP1:/tmp # ifconfig eth0 | grep "inet" | grep -v "inet6" | tr -s " " ":" | cut -d: -f4 10.0.2.15
[stextbox id='black' image='null'] Important : Note the use of the -s switch with the tr command. This switch replaces a string of x identical characters with a single character. [/stextbox]
<html>
Copyright © 2011-2018 Hugh Norris.<br><br>
</html>