Table des matières

Last updated on: 2020/01/30 03:27

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@debian8:~$ stty -a
speed 38400 baud; rows 23; 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@debian8:~$ date
Fri 19 Aug 04:44:26 BST 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@debian8:~$ who
trainee  pts/0        2016-08-18 23:33 (10.0.2.2)

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@debian8:~$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1        9947060 3386724   6031956  36% /
udev               10240       0     10240   0% /dev
tmpfs             101256    4796     96460   5% /run
tmpfs             253132       0    253132   0% /dev/shm
tmpfs               5120       4      5116   1% /run/lock
tmpfs             253132       0    253132   0% /sys/fs/cgroup
tmpfs              50628       0     50628   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@debian8:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       9.5G  3.3G  5.8G  36% /
udev             10M     0   10M   0% /dev
tmpfs            99M  4.7M   95M   5% /run
tmpfs           248M     0  248M   0% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           248M     0  248M   0% /sys/fs/cgroup
tmpfs            50M     0   50M   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@debian8:~$ free
             total       used       free     shared    buffers     cached
Mem:        506268     499724       6544       3048     125976     121412
-/+ buffers/cache:     252336     253932
Swap:      2046972       1756    2045216

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

trainee@debian8:~$ free -h
             total       used       free     shared    buffers     cached
Mem:          494M       488M       6.3M       3.0M       123M       118M
-/+ buffers/cache:       246M       247M
Swap:         2.0G       1.7M       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@debian8:~$ whoami
trainee

Now become the system administrator root:

trainee@debian8:~$ su -
Password: fenestros
root@debian8:~#

Important : Note that the password will not be visible.

Now use the whoami command again:

root@debian8:~# whoami
root
root@debian8:~#  

Important : Note the current effective user ID is root.

Finally execute the exit command to return to trainee:

root@debian8:~# exit
logout
trainee@debian8:~

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@debian8:~$ 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@debian8:~$ cd /tmp
trainee@debian8:/tmp$ pwd
/tmp
trainee@debian8:/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@debian8:/tmp$ ls
hsperfdata_root  inode  pulse-PKdhtXMmr18n

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@debian8:/tmp$ su -
Password: fenestros
root@debian8:~# lsof | more
COMMAND     PID   TID        USER   FD      TYPE             DEVICE SIZE/OFF       NODE NAME
systemd       1              root  cwd       DIR                8,1     4096          2 /
systemd       1              root  rtd       DIR                8,1     4096          2 /
systemd       1              root  txt       REG                8,1  1309064      26423 /lib/systemd/systemd
systemd       1              root  mem       REG                8,1    18640      26081 /lib/x86_64-linux-gnu/libattr.so.1.1.0
systemd       1              root  mem       REG                8,1    14664      26064 /lib/x86_64-linux-gnu/libdl-2.19.so
systemd       1              root  mem       REG                8,1   448440      26091 /lib/x86_64-linux-gnu/libpcre.so.3.13.1
systemd       1              root  mem       REG                8,1    31784      26076 /lib/x86_64-linux-gnu/librt-2.19.so
systemd       1              root  mem       REG                8,1    92888      26135 /lib/x86_64-linux-gnu/libkmod.so.2.2.8
systemd       1              root  mem       REG                8,1    19016      26113 /lib/x86_64-linux-gnu/libcap.so.2.24
systemd       1              root  mem       REG                8,1   113024      26093 /lib/x86_64-linux-gnu/libaudit.so.1.0.0
systemd       1              root  mem       REG                8,1    64024      26096 /lib/x86_64-linux-gnu/libpam.so.0.83.1
systemd       1              root  mem       REG                8,1   142728      26137 /lib/x86_64-linux-gnu/libselinux.so.1
systemd       1              root  mem       REG                8,1  1738176      26061 /lib/x86_64-linux-gnu/libc-2.19.so
systemd       1              root  mem       REG                8,1   137440      26041 /lib/x86_64-linux-gnu/libpthread-2.19.so
systemd       1              root  mem       REG                8,1   140928      26058 /lib/x86_64-linux-gnu/ld-2.19.so
systemd       1              root    0u      CHR                1,3      0t0       5593 /dev/null
systemd       1              root    1u      CHR                1,3      0t0       5593 /dev/null
systemd       1              root    2u      CHR                1,3      0t0       5593 /dev/null
systemd       1              root    3w      CHR               1,11      0t0       5599 /dev/kmsg
systemd       1              root    4u     0000                0,9        0       5589 anon_inode
systemd       1              root    5u     0000                0,9        0       5589 anon_inode
systemd       1              root    6r      DIR               0,20        0          1 /sys/fs/cgroup/systemd
--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@debian8:~# exit
logout
trainee@debian8:/tmp$ touch test
trainee@debian8:/tmp$ ls
hsperfdata_root  inode  pulse-PKdhtXMmr18n  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@debian8:/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@debian8:/tmp$ cp test ~
trainee@debian8:/tmp$ ls -l ~
total 36
drwxr-xr-x 2 trainee trainee 4096 May  1 20:42 Desktop
drwxr-xr-x 2 trainee trainee 4096 May  1 20:42 Documents
drwxr-xr-x 2 trainee trainee 4096 May  1 20:42 Downloads
drwxr-xr-x 2 trainee trainee 4096 May  1 20:42 Music
drwxr-xr-x 2 trainee trainee 4096 May  1 20:42 Pictures
drwxr-xr-x 2 trainee trainee 4096 May  1 20:42 Public
drwxr-xr-x 2 trainee trainee 4096 May  1 20:42 Templates
-rw-r--r-- 1 trainee trainee    0 Aug 19 16:14 test
drwxr-xr-x 2 trainee trainee 4096 May  1 20:42 Videos
-rw-r--r-- 1 trainee trainee  391 Aug 18 23:34 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@debian8:/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@debian8:/tmp$ echo "fenestros" > ~/test

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

trainee@debian8:/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@debian8:/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@debian8:/tmp$ mv ~/test .
trainee@debian8:/tmp$ ls -l ~
total 36
drwxr-xr-x 2 trainee trainee 4096 May  1 20:42 Desktop
drwxr-xr-x 2 trainee trainee 4096 May  1 20:42 Documents
drwxr-xr-x 2 trainee trainee 4096 May  1 20:42 Downloads
drwxr-xr-x 2 trainee trainee 4096 May  1 20:42 Music
drwxr-xr-x 2 trainee trainee 4096 May  1 20:42 Pictures
drwxr-xr-x 2 trainee trainee 4096 May  1 20:42 Public
drwxr-xr-x 2 trainee trainee 4096 May  1 20:42 Templates
drwxr-xr-x 2 trainee trainee 4096 May  1 20:42 Videos
-rw-r--r-- 1 trainee trainee  391 Aug 18 23:34 vitext
trainee@debian8:/tmp$ mv test TeSt
trainee@debian8:/tmp$ ls -l
total 16
drwxr-xr-x 2 root    root    4096 Aug 18 11:33 hsperfdata_root
drwxr-xr-x 2 root    root    4096 Aug 18 13:36 inode
drwx------ 2 root    root    4096 Aug 18 11:29 pulse-PKdhtXMmr18n
-rw-r--r-- 1 trainee trainee   10 Aug 19 16:20 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@debian8:/tmp$ cd ~
trainee@debian8:~$ mkdir testdir
trainee@debian8:~$ 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@debian8:~$ rmdir testdir
trainee@debian8:~$ 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@debian8:~$ mkdir testdir1
trainee@debian8:~$ cd /tmp
trainee@debian8:/tmp$ echo "fenestros" > TeSt
trainee@debian8:/tmp$ cd ~
trainee@debian8:~$ mv /tmp/TeSt ~/testdir1
trainee@debian8:~$ ls -lR testdir1/
testdir1/:
total 4
-rw-r--r-- 1 trainee trainee 10 Aug 19 17:01 TeSt
trainee@debian8:~$ rmdir testdir1/
rmdir: failed to remove ‘testdir1/’: Directory not empty
trainee@debian8:~$ rm -rf testdir1/
trainee@debian8:~$ 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@debian8:~$ touch aac abc bca xyz
trainee@debian8:~$ ls
aac  bca      Documents  Music     Public     Videos  xyz
abc  Desktop  Downloads  Pictures  Templates  vitext
trainee@debian8:~$ ls | sort
aac
abc
bca
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos
vitext
xyz
trainee@debian8:~$ 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@debian8:~$ more /etc/services
# Network services, Internet style
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, officially ports have two entries
# even if the protocol doesn't support UDP operations.
#
# Updated from http://www.iana.org/assignments/port-numbers and other
# sources like http://www.freebsd.org/cgi/cvsweb.cgi/src/etc/services .
# New ports will be added on request if they have been officially assigned
# by IANA and used in the real-world or are needed by a debian package.
# If you need a huge list of used numbers please install the nmap package.

tcpmux		1/tcp				# TCP port service multiplexer
echo		7/tcp
echo		7/udp
discard		9/tcp		sink null
discard		9/udp		sink null
systat		11/tcp		users
daytime		13/tcp
daytime		13/udp
netstat		15/tcp
qotd		17/tcp		quote
msp		18/tcp				# message send protocol
--More--(4%)

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@debian8:~$ less /etc/services
# Network services, Internet style
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, officially ports have two entries
# even if the protocol doesn't support UDP operations.
#
# Updated from http://www.iana.org/assignments/port-numbers and other
# sources like http://www.freebsd.org/cgi/cvsweb.cgi/src/etc/services .
# New ports will be added on request if they have been officially assigned
# by IANA and used in the real-world or are needed by a debian package.
# If you need a huge list of used numbers please install the nmap package.

tcpmux          1/tcp                           # TCP port service multiplexer
echo            7/tcp
echo            7/udp
discard         9/tcp           sink null
discard         9/udp           sink null
systat          11/tcp          users
daytime         13/tcp
daytime         13/udp
netstat         15/tcp
qotd            17/tcp          quote
msp             18/tcp                          # message send protocol
/etc/services

Command Line Switches

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@debian8:~$ find acc
find: `acc': No such file or directory
trainee@debian8:~$ 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@debian8:~$ su -
Password: fenestros

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@debian8:~# ls -l /var/lib/mlocate/mlocate.db 
-rw-r----- 1 root mlocate 2067895 Aug 19 07:35 /var/lib/mlocate/mlocate.db

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

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

Use of these two commands is very simple:

root@debian8:~# updatedb
root@debian8:~# locate aac
/home/trainee/aac
/lib/modules/3.16.0-4-amd64/kernel/drivers/scsi/aacraid
/lib/modules/3.16.0-4-amd64/kernel/drivers/scsi/aacraid/aacraid.ko
/usr/lib/x86_64-linux-gnu/libaacs.so.0
/usr/lib/x86_64-linux-gnu/libaacs.so.0.4.1
/usr/share/doc/libaacs0
/usr/share/doc/libaacs0/KEYDB.cfg.gz
/usr/share/doc/libaacs0/changelog.Debian.amd64.gz
/usr/share/doc/libaacs0/changelog.Debian.gz
/usr/share/doc/libaacs0/changelog.gz
/usr/share/doc/libaacs0/copyright
/usr/share/mime/audio/aac.xml
/var/cache/apt/archives/libaacs0_0.7.1-1+b1_amd64.deb
/var/lib/dpkg/info/libaacs0:amd64.list
/var/lib/dpkg/info/libaacs0:amd64.md5sums
/var/lib/dpkg/info/libaacs0:amd64.postinst
/var/lib/dpkg/info/libaacs0:amd64.postrm
/var/lib/dpkg/info/libaacs0:amd64.shlibs

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@debian8:~# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1ssl.gz /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@debian8:~# which passwd
/usr/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@debian8:~# uptime
 18:02:14 up 1 day,  6:33,  1 user,  load average: 0.00, 0.01, 0.05

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@debian8:~# w
 18:11:31 up 1 day,  6:42,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
trainee  pts/0    10.0.2.2         14:16    0.00s  0.13s  0.03s 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@debian8:~# uname -a
Linux debian8 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt25-2 (2016-04-08) x86_64 GNU/Linux
root@debian8:~# uname -s
Linux
root@debian8:~# uname -n
debian8
root@debian8:~# uname -r
3.16.0-4-amd64
root@debian8:~# uname -v
#1 SMP Debian 3.16.7-ckt25-2 (2016-04-08)
root@debian8:~# uname -m
x86_64
root@debian8:~# uname -p
unknown
root@debian8:~# uname -i
unknown
root@debian8:~# 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@debian8:~# du -sh /* 2>/dev/null
12M	/bin
31M	/boot
0	/dev
6.9M	/etc
1.9M	/home
0	/initrd.img
207M	/lib
4.0K	/lib64
16K	/lost+found
8.0K	/media
4.0K	/mnt
152M	/opt
0	/proc
59M	/root
4.7M	/run
8.5M	/sbin
4.0K	/srv
0	/sys
100K	/tmp
2.0G	/usr
767M	/var
0	/vmlinuz

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@debian8:~# clear

root@debian8:~# 

The exit Command

This command exits the current shell:

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

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

When using Debian 8, 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@debian8:~$ su -
Password: fenestros
root@debian8:~# wall this is a message from root
                                                                               
Broadcast message from trainee@debian8 (pts/0) (Sat Aug 20 03:23:20 2016):     
                                                                               
this is a message from root
                                                                               
root@debian8:~# 

In the second session you should see the following message :

Broadcast message from trainee@debian8 (pts/0) (Sat Aug 20 03:23:20 2016):     
                                                                               
this is a message from root                                                                               

The seq Command

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

  • seq [OPTION]… LAST
  • seq [OPTION]… FIRST LAST
  • seq [OPTION]… FIRST INCREMENT LAST

For example :

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

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 Debian 8, Ubuntu 16.04 LTS and RHEL/CentOS 7. Use the appropriate package manager to install it:

root@debian8:~# which screen
root@debian8:~# apt-get install screen
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  iselect screenie byobu
The following NEW packages will be installed:
  screen
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 569 kB of archives.
After this operation, 930 kB of additional disk space will be used.
Get:1 http://ftp.fr.debian.org/debian/ jessie/main screen amd64 4.2.1-3+deb8u1 [569 kB]
Fetched 569 kB in 0s (3,163 kB/s)
Selecting previously unselected package screen.
(Reading database ... 82450 files and directories currently installed.)
Preparing to unpack .../screen_4.2.1-3+deb8u1_amd64.deb ...
Unpacking screen (4.2.1-3+deb8u1) ...
Processing triggers for systemd (215-17+deb8u4) ...
Processing triggers for install-info (5.2.0.dfsg.1-6) ...
Processing triggers for man-db (2.7.0.2-5) ...
Setting up screen (4.2.1-3+deb8u1) ...
Processing triggers for systemd (215-17+deb8u4) ...
root@debian8:~# which screen
/usr/bin/screen

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

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

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

root@debian8:~# screen -ls
There is a screen on:
        27309.mysession (20/08/16 04:20:08)     (Attached)
1 Socket in /var/run/screen/S-root.

Now enter the following commands:

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

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

root@debian8:~# screen -S mysession
[detached from 27309.mysession]
root@debian8:~# 

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

root@debian8:~# screen -r

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

root@debian8:~# 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@debian8:~# screen -S mysession
[detached from 27309.mysession] key
root@debian8:~# 

Now create a new, non-nested screen:

root@debian8:~# screen -S mysession1

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

root@debian8:~# screen -ls
There are screens on:
        10234.mysession1        (20/08/16 07:17:19)     (Attached)
        27309.mysession (20/08/16 04:20:08)     (Detached)
2 Sockets in /var/run/screen/S-root.

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

root@debian8:~# screen -r 27309

Finally, check out what has happened:

root@debian8:~# sleep 9999 &
[1] 2941
root@debian8:~# jobs
[1]+  Running                 sleep 9999 &unit
root@debian8:~# jobs
[1]+  Running                 sleep 9999 &
root@debian8:~# screen -ls
There are screens on:
        10234.mysession1        (20/08/16 07:17:20)     (Attached)
        27309.mysession (20/08/16 04:20:09)     (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@debian8:~# ls -lai /tmp
total 40
130564 drwxrwxrwt 10 root root 4096 Aug 20 09:17 .
     2 drwxr-xr-x 22 root root 4096 Aug  1 12:38 ..
130572 drwxrwxrwt  2 root root 4096 Aug 18 11:28 .font-unix
134332 drwxr-xr-x  2 root root 4096 Aug 18 11:33 hsperfdata_root
130570 drwxrwxrwt  2 root root 4096 Aug 18 11:28 .ICE-unix
140612 drwxr-xr-x  2 root root 4096 Aug 18 13:36 inode
140574 srw-------  1 root root    0 Aug 18 11:32 .java_pid1704
140611 srw-------  1 root root    0 Aug 18 11:33 .java_pid2098
134331 drwx------  2 root root 4096 Aug 18 11:29 pulse-PKdhtXMmr18n
130573 drwxrwxrwt  2 root root 4096 Aug 18 11:28 .Test-unix
130569 drwxrwxrwt  2 root root 4096 Aug 18 11:28 .X11-unix
130571 drwxrwxrwt  2 root root 4096 Aug 18 11:28 .XIM-unix
root@debian8:~# ls -ali /tmp
total 40
130564 drwxrwxrwt 10 root root 4096 Aug 20 09:17 .
     2 drwxr-xr-x 22 root root 4096 Aug  1 12:38 ..
130572 drwxrwxrwt  2 root root 4096 Aug 18 11:28 .font-unix
134332 drwxr-xr-x  2 root root 4096 Aug 18 11:33 hsperfdata_root
130570 drwxrwxrwt  2 root root 4096 Aug 18 11:28 .ICE-unix
140612 drwxr-xr-x  2 root root 4096 Aug 18 13:36 inode
140574 srw-------  1 root root    0 Aug 18 11:32 .java_pid1704
140611 srw-------  1 root root    0 Aug 18 11:33 .java_pid2098
134331 drwx------  2 root root 4096 Aug 18 11:29 pulse-PKdhtXMmr18n
130573 drwxrwxrwt  2 root root 4096 Aug 18 11:28 .Test-unix
130569 drwxrwxrwt  2 root root 4096 Aug 18 11:28 .X11-unix
130571 drwxrwxrwt  2 root root 4096 Aug 18 11:28 .XIM-unix

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

root@debian8:~# ls -l --all --inode /tmp
total 40
130564 drwxrwxrwt 10 root root 4096 Aug 20 09:17 .
     2 drwxr-xr-x 22 root root 4096 Aug  1 12:38 ..
130572 drwxrwxrwt  2 root root 4096 Aug 18 11:28 .font-unix
134332 drwxr-xr-x  2 root root 4096 Aug 18 11:33 hsperfdata_root
130570 drwxrwxrwt  2 root root 4096 Aug 18 11:28 .ICE-unix
140612 drwxr-xr-x  2 root root 4096 Aug 18 13:36 inode
140574 srw-------  1 root root    0 Aug 18 11:32 .java_pid1704
140611 srw-------  1 root root    0 Aug 18 11:33 .java_pid2098
134331 drwx------  2 root root 4096 Aug 18 11:29 pulse-PKdhtXMmr18n
130573 drwxrwxrwt  2 root root 4096 Aug 18 11:28 .Test-unix
130569 drwxrwxrwt  2 root root 4096 Aug 18 11:28 .X11-unix
130571 drwxrwxrwt  2 root root 4096 Aug 18 11:28 .XIM-unix

root@debian8:~# 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@debian8:~# mv /home/trainee/Downloads/greptest /tmp/greptest

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

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

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

root@debian8:~# 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@debian8:~# 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@debian8:~# 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@debian8:~# egrep -v '^(#|$)'  /tmp/greptest > /tmp/greptest1
root@debian8:~# 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@debian8:~# mv /home/trainee/Downloads/greptest /tmp/greptest

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

root@debian8:~# 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@debian8:~# 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@debian8:~# 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@debian8:~# sed '1,10d' /etc/services | more
# If you need a huge list of used numbers please install the nmap package.

tcpmux		1/tcp				# TCP port service multiplexer
echo		7/tcp
echo		7/udp
discard		9/tcp		sink null
discard		9/udp		sink null
systat		11/tcp		users
daytime		13/tcp
daytime		13/udp
netstat		15/tcp
qotd		17/tcp		quote
msp		18/tcp				# message send protocol
msp		18/udp
chargen		19/tcp		ttytst source
chargen		19/udp		ttytst source
ftp-data	20/tcp
ftp		21/tcp
fsp		21/udp		fspd
ssh		22/tcp				# SSH Remote Login Protocol
ssh		22/udp
telnet		23/tcp
smtp		25/tcp		mail
--More--                   

Now display the same file without any commented lines:

root@debian8:~# sed '/^#/d' /etc/services | more

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

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@debian8:~# sed '1,2p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
...

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@debian8:~#  sed -n '1,2p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin

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

root@debian8:~# sed -n '/^#/!w /tmp/sedtest' /etc/services
root@debian8:~# more /tmp/sedtest

tcpmux		1/tcp				# TCP port service multiplexer
echo		7/tcp
echo		7/udp
discard		9/tcp		sink null
discard		9/udp		sink null
systat		11/tcp		users
daytime		13/tcp
daytime		13/udp
netstat		15/tcp
qotd		17/tcp		quote
msp		18/tcp				# message send protocol
msp		18/udp
chargen		19/tcp		ttytst source
chargen		19/udp		ttytst source
ftp-data	20/tcp
ftp		21/tcp
fsp		21/udp		fspd
ssh		22/tcp				# SSH Remote Login Protocol
ssh		22/udp
telnet		23/tcp
smtp		25/tcp		mail
time		37/tcp		timserver
--More--(2%)

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@debian8:~# echo "user1,user2,user3" > /tmp/sedtest1
root@debian8:~# 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@debian8:~# ls -l | awk '{print $8 $3 $4}'

2016rootroot
2016rootroot
2016rootroot
2016rootroot
2016rootroot

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

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

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

Conditions

A regular expression applied to a record
  • Format:
    • /regular expression/ {action}
  • Exemple:
    • /hello/ {print $0}
A regular expression applied to a field
  • Format:
    • $n ~/regular expression/ {action}
    • $n!~/regular expression/ {action}
  • Examples:
    • $1 ~/hello/ {print $0}
    • $1!~/hello/ {print $0}
Comparisons
  • Format:
    • $n operator criteria {action}
  • Example:
    • $1 > 20 {print $0}

Operators

Operator Condition
< Less than
Less than or equal to
== Equal to
!= Different
> Greater than
>= Greater than or equal to
Logical Operators
  • Format:
    • test1 logical operator test2 {action}
  • Example:
    • $1 ~/hello/ && $2 > 20 {print $0}

Operators

Operator Condition
|| OR
&& AND
! NO
Built-in Variables
  • Format:
    • expression1, expression2 {instruction}
  • Example:
    • NR==7, NR==10 {print $0}

Variables

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

Awk Scripts

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

  • BEGIN
    • This section is executed once, prior to executing the body of the script
  • BODY
    • This sections contains the clauses to be applied to each line
  • END
    • This section is executed once, after executing the body of the script

For example:

root@debian8:~# 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@debian8:~# awk -f awkscript /etc/fstab
List of the currently mounted file systems
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda1 during installation
UUID=4a230056-285f-42f4-bfe0-5a73dbc5b745 /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda5 during installation
UUID=da5a77e9-344d-42aa-aed6-a38d381ba436 none            swap    sw              0       0
/dev/sr0        /media/cdrom0   udf,iso9660 user,noauto     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@debian8:~# 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@debian8:~#  mv /home/trainee/Downloads/expand.txt /root/expand

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

root@debian8:~#  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@debian8:~#  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@debian8:~#  expand expand > expand1

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

root@debian8:~#  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@debian8:~#  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@debian8:~#  unexpand -a expand1 > expand2
root@debian8:~#  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@debian8:~# cut -c1-7 /etc/passwd
root:x:
daemon:
bin:x:2
sys:x:3
sync:x:
games:x
man:x:6
lp:x:7:
mail:x:
news:x:
uucp:x:
proxy:x
www-dat
backup:
list:x:
irc:x:3
gnats:x
nobody:
systemd
systemd
systemd
systemd
trainee
sshd:x:
Debian-
message
statd:x
avahi-a
avahi:x
colord:
dnsmasq
speech-
pulse:x
rtkit:x
saned:x
usbmux:
lightdm

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

root@debian8:~# cut -c1-5,10-15,30- /etc/passwd
root:0:rootsh
daemo1:1:da:/usr/sbin/nologin
bin:x:bin:/nologin
sys:x:sys:/nologin
sync:65534:/sync
games:60:ga:/usr/sbin/nologin
man:x2:man::/usr/sbin/nologin
lp:x:lp:/vasr/sbin/nologin
mail:8:mailr/sbin/nologin
news:9:newsws:/usr/sbin/nologin
uucp::10:uuuucp:/usr/sbin/nologin
proxy3:13:p/sbin/nologin
www-dx:33:3r/www:/usr/sbin/nologin
backu34:34:ckups:/usr/sbin/nologin
list::38:Maager:/var/list:/usr/sbin/nologin
irc:x39:ircd:/usr/sbin/nologin
gnats1:41:Gting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobod65534:nonexistent:/usr/sbin/nologin
systeimesynstemd Time Synchronization,,,:/run/systemd:/bin/false
systeetworktemd Network Management,,,:/run/systemd/netif:/bin/false
systeesolvetemd Resolver,,,:/run/systemd/resolve:/bin/false
systeus-proystemd Bus Proxy,,,:/run/systemd:/bin/false
train:1000:,:/home/trainee:/bin/bash
sshd:4:6553hd:/usr/sbin/nologin
Debiaim:x:1pool/exim4:/bin/false
messas:x:10n/dbus:/bin/false
statd07:655fs:/bin/false
avahioipd:x autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false
avahi09:115emon,,,:/var/run/avahi-daemon:/bin/false
color110:11r management daemon,,,:/var/lib/colord:/bin/false
dnsma:111:6,:/var/lib/misc:/bin/false
speecspatcheech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh
pulse13:119emon,,,:/var/run/pulse:/bin/false
rtkit14:121,:/proc:/bin/false
saned15:122ed:/bin/false
usbmu116:46,,,:/var/lib/usbmux:/bin/false
light:117:1ay Manager:/var/lib/lightdm:/bin/false

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

root@debian8:~# cut -d: -f2,4,6 /etc/passwd
x:0:/root
x:1:/usr/sbin
x:2:/bin
x:3:/dev
x:65534:/bin
x:60:/usr/games
x:12:/var/cache/man
x:7:/var/spool/lpd
x:8:/var/mail
x:9:/var/spool/news
x:10:/var/spool/uucp
x:13:/bin
x:33:/var/www
x:34:/var/backups
x:38:/var/list
x:39:/var/run/ircd
x:41:/var/lib/gnats
x:65534:/nonexistent
x:103:/run/systemd
x:104:/run/systemd/netif
x:105:/run/systemd/resolve
x:106:/run/systemd
x:1000:/home/trainee
x:65534:/var/run/sshd
x:110:/var/spool/exim4
x:111:/var/run/dbus
x:65534:/var/lib/nfs
x:113:/var/lib/avahi-autoipd
x:115:/var/run/avahi-daemon
x:117:/var/lib/colord
x:65534:/var/lib/misc
x:29:/var/run/speech-dispatcher
x:119:/var/run/pulse
x:121:/proc
x:122:/var/lib/saned
x:46:/var/lib/usbmux
x:124:/var/lib/lightdm

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@debian8:~# cut -d: -f4 /etc/passwd | sort -n | uniq
0
1
2
3
7
8
9
10
12
13
29
33
34
38
39
41
46
60
103
104
105
106
110
111
113
115
117
119
121
122
124
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@debian8:~# cat /etc/passwd | tr "[a-z]" "[A-Z]"
ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
DAEMON:X:1:1:DAEMON:/USR/SBIN:/USR/SBIN/NOLOGIN
BIN:X:2:2:BIN:/BIN:/USR/SBIN/NOLOGIN
SYS:X:3:3:SYS:/DEV:/USR/SBIN/NOLOGIN
SYNC:X:4:65534:SYNC:/BIN:/BIN/SYNC
GAMES:X:5:60:GAMES:/USR/GAMES:/USR/SBIN/NOLOGIN
MAN:X:6:12:MAN:/VAR/CACHE/MAN:/USR/SBIN/NOLOGIN
LP:X:7:7:LP:/VAR/SPOOL/LPD:/USR/SBIN/NOLOGIN
MAIL:X:8:8:MAIL:/VAR/MAIL:/USR/SBIN/NOLOGIN
NEWS:X:9:9:NEWS:/VAR/SPOOL/NEWS:/USR/SBIN/NOLOGIN
UUCP:X:10:10:UUCP:/VAR/SPOOL/UUCP:/USR/SBIN/NOLOGIN
PROXY:X:13:13:PROXY:/BIN:/USR/SBIN/NOLOGIN
WWW-DATA:X:33:33:WWW-DATA:/VAR/WWW:/USR/SBIN/NOLOGIN
BACKUP:X:34:34:BACKUP:/VAR/BACKUPS:/USR/SBIN/NOLOGIN
LIST:X:38:38:MAILING LIST MANAGER:/VAR/LIST:/USR/SBIN/NOLOGIN
IRC:X:39:39:IRCD:/VAR/RUN/IRCD:/USR/SBIN/NOLOGIN
GNATS:X:41:41:GNATS BUG-REPORTING SYSTEM (ADMIN):/VAR/LIB/GNATS:/USR/SBIN/NOLOGIN
NOBODY:X:65534:65534:NOBODY:/NONEXISTENT:/USR/SBIN/NOLOGIN
SYSTEMD-TIMESYNC:X:100:103:SYSTEMD TIME SYNCHRONIZATION,,,:/RUN/SYSTEMD:/BIN/FALSE
SYSTEMD-NETWORK:X:101:104:SYSTEMD NETWORK MANAGEMENT,,,:/RUN/SYSTEMD/NETIF:/BIN/FALSE
SYSTEMD-RESOLVE:X:102:105:SYSTEMD RESOLVER,,,:/RUN/SYSTEMD/RESOLVE:/BIN/FALSE
SYSTEMD-BUS-PROXY:X:103:106:SYSTEMD BUS PROXY,,,:/RUN/SYSTEMD:/BIN/FALSE
TRAINEE:X:1000:1000:TRAINEE,,,:/HOME/TRAINEE:/BIN/BASH
SSHD:X:104:65534::/VAR/RUN/SSHD:/USR/SBIN/NOLOGIN
DEBIAN-EXIM:X:105:110::/VAR/SPOOL/EXIM4:/BIN/FALSE
MESSAGEBUS:X:106:111::/VAR/RUN/DBUS:/BIN/FALSE
STATD:X:107:65534::/VAR/LIB/NFS:/BIN/FALSE
AVAHI-AUTOIPD:X:108:113:AVAHI AUTOIP DAEMON,,,:/VAR/LIB/AVAHI-AUTOIPD:/BIN/FALSE
AVAHI:X:109:115:AVAHI MDNS DAEMON,,,:/VAR/RUN/AVAHI-DAEMON:/BIN/FALSE
COLORD:X:110:117:COLORD COLOUR MANAGEMENT DAEMON,,,:/VAR/LIB/COLORD:/BIN/FALSE
DNSMASQ:X:111:65534:DNSMASQ,,,:/VAR/LIB/MISC:/BIN/FALSE
SPEECH-DISPATCHER:X:112:29:SPEECH DISPATCHER,,,:/VAR/RUN/SPEECH-DISPATCHER:/BIN/SH
PULSE:X:113:119:PULSEAUDIO DAEMON,,,:/VAR/RUN/PULSE:/BIN/FALSE
RTKIT:X:114:121:REALTIMEKIT,,,:/PROC:/BIN/FALSE
SANED:X:115:122::/VAR/LIB/SANED:/BIN/FALSE
USBMUX:X:116:46:USBMUX DAEMON,,,:/VAR/LIB/USBMUX:/BIN/FALSE
LIGHTDM:X:117:124:LIGHT DISPLAY MANAGER:/VAR/LIB/LIGHTDM:/BIN/FALSE
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@debian8:~# paste -d: /etc/passwd /etc/shadow
root:x:0:0:root:/root:/bin/bash:root:$6$jxZolPSj$yHWmlcj8imvAXDzAoxZG3KEILb3ITQJgTPhqqLsUqjIuUrfc5Yhzrfl9KrVARr16U0MZWTdqhb.FaSsRpfRRC1:16922:0:99999:7:::
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin:daemon:*:16922:0:99999:7:::
bin:x:2:2:bin:/bin:/usr/sbin/nologin:bin:*:16922:0:99999:7:::
sys:x:3:3:sys:/dev:/usr/sbin/nologin:sys:*:16922:0:99999:7:::
sync:x:4:65534:sync:/bin:/bin/sync:sync:*:16922:0:99999:7:::
games:x:5:60:games:/usr/games:/usr/sbin/nologin:games:*:16922:0:99999:7:::
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin:man:*:16922:0:99999:7:::
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin:lp:*:16922:0:99999:7:::
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin:mail:*:16922:0:99999:7:::
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin:news:*:16922:0:99999:7:::
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin:uucp:*:16922:0:99999:7:::
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin:proxy:*:16922:0:99999:7:::
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin:www-data:*:16922:0:99999:7:::
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin:backup:*:16922:0:99999:7:::
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin:list:*:16922:0:99999:7:::
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin:irc:*:16922:0:99999:7:::
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin:gnats:*:16922:0:99999:7:::
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin:nobody:*:16922:0:99999:7:::
systemd-timesync:x:100:103:systemd Time Synchronization,,,:/run/systemd:/bin/false:systemd-timesync:*:16922:0:99999:7:::
systemd-network:x:101:104:systemd Network Management,,,:/run/systemd/netif:/bin/false:systemd-network:*:16922:0:99999:7:::
systemd-resolve:x:102:105:systemd Resolver,,,:/run/systemd/resolve:/bin/false:systemd-resolve:*:16922:0:99999:7:::
systemd-bus-proxy:x:103:106:systemd Bus Proxy,,,:/run/systemd:/bin/false:systemd-bus-proxy:*:16922:0:99999:7:::
trainee:x:1000:1000:trainee,,,:/home/trainee:/bin/bash:trainee:$6$7S6OreWb$t92JjORxWDcNyF.DtQVAotCxsWfRsvIQUaWjcNfKX3OoFh2GlX2dhtx.oeA0xEjqbff2OPb51VeVjBgwdPU4R0:16922:0:99999:7:::
sshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin:sshd:*:16922:0:99999:7:::
Debian-exim:x:105:110::/var/spool/exim4:/bin/false:Debian-exim:!:16922:0:99999:7:::
messagebus:x:106:111::/var/run/dbus:/bin/false:messagebus:*:16922:0:99999:7:::
statd:x:107:65534::/var/lib/nfs:/bin/false:statd:*:16922:0:99999:7:::
avahi-autoipd:x:108:113:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false:avahi-autoipd:*:16922:0:99999:7:::
avahi:x:109:115:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false:avahi:*:16922:0:99999:7:::
colord:x:110:117:colord colour management daemon,,,:/var/lib/colord:/bin/false:colord:*:16922:0:99999:7:::
dnsmasq:x:111:65534:dnsmasq,,,:/var/lib/misc:/bin/false:dnsmasq:*:16922:0:99999:7:::
speech-dispatcher:x:112:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh:speech-dispatcher:!:16922:0:99999:7:::
pulse:x:113:119:PulseAudio daemon,,,:/var/run/pulse:/bin/false:pulse:*:16922:0:99999:7:::
rtkit:x:114:121:RealtimeKit,,,:/proc:/bin/false:rtkit:*:16922:0:99999:7:::
saned:x:115:122::/var/lib/saned:/bin/false:saned:*:16922:0:99999:7:::
usbmux:x:116:46:usbmux daemon,,,:/var/lib/usbmux:/bin/false:usbmux:*:16922:0:99999:7:::
lightdm:x:117:124:Light Display Manager:/var/lib/lightdm:/bin/false:lightdm:*:16922:0:99999:7:::

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@debian8:~# dd if=/dev/zero of=/file bs=1024k count=250
250+0 records in
250+0 records out
262144000 bytes (262 MB) copied, 0.57181 s, 458 MB/s

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

root@debian8:~# split -b 50m /file filepart
root@debian8:~# ls -l | grep filepart
-rw-r--r-- 1 root root 52428800 Nov 30 18:41 filepartaa
-rw-r--r-- 1 root root 52428800 Nov 30 18:41 filepartab
-rw-r--r-- 1 root root 52428800 Nov 30 18:41 filepartac
-rw-r--r-- 1 root root 52428800 Nov 30 18:41 filepartad
-rw-r--r-- 1 root root 52428800 Nov 30 18:41 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@debian8:~# cat fileparta* > newfile
root@debian8:~# ls -l | grep newf
-rw-r--r-- 1 root root 262144000 Nov 30 18:42 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 files 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@debian8:~# cp /etc/passwd /root

Edit the */root/passwd file as shown:

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

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

...
Linux is great!

Now compare the two files:

root@debian8:~# diff /etc/passwd /root/passwd
8d7
< lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
23c22
< trainee:x:1000:1000:trainee,,,:/home/trainee:/bin/bash
---
> trainee10:x:1000:1000:trainee,,,:/home/trainee:/bin/bash
37a37
> Linux is great! 

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

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

The output 23c22 means that at line 23 in /etc/passwd needs to be modified in order to be the same as line 22 in /root/passwd.

The output 37a37 means that line 37 in /root/passwd needs to be added after line 37 in /etc/passwd.

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@debian8:~# cmp /root/passwd /etc/passwd
/root/passwd /etc/passwd differ: byte 286, line 8

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

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

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

Command Line Switches

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.

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

root@debian8:~#  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@debian8:~# cd /tmp
root@debian8:/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@debian8:/tmp# cat /tmp/greptest.patch
--- greptest	2016-11-30 18:54:52.732000000 +0000
+++ greptest1	2016-11-30 18:55:28.392000000 +0000
@@ -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@debian8:/tmp# patch < greptest.patch
patching file greptest

Finally, check the contents of the patched greptest file:

root@debian8:/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.

The strings command is not installed by default in Debian 8 :

root@debian8:/tmp# apt-get install binutils
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  binutils-doc
The following NEW packages will be installed:
  binutils
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 3,516 kB of archives.
After this operation, 21.1 MB of additional disk space will be used.
Get:1 http://ftp.fr.debian.org/debian/ jessie/main binutils amd64 2.25-5 [3,516 kB]
Fetched 3,516 kB in 0s (7,674 kB/s)
Selecting previously unselected package binutils.
(Reading database ... 82508 files and directories currently installed.)
Preparing to unpack .../binutils_2.25-5_amd64.deb ...
Unpacking binutils (2.25-5) ...
Processing triggers for man-db (2.7.0.2-5) ...
Setting up binutils (2.25-5) ...
Processing triggers for libc-bin (2.19-18+deb8u4) ...
root@debian8:/tmp# which strings
/usr/bin/strings

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

root@debian8:/tmp# strings /usr/bin/passwd | more
/lib64/ld-linux-x86-64.so.2
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
libaudit.so.1
_fini
_init
libselinux.so.1
is_selinux_enabled
security_getenforce
context_user_get
security_compute_av
matchpathcon
freecon
context_free
--More--

Print the offset within the file before each string:

root@debian8:/tmp# strings -t d /usr/bin/passwd | more
    568 /lib64/ld-linux-x86-64.so.2
   3985 libpam.so.0
   3997 _ITM_deregisterTMCloneTable
   4025 __gmon_start__
   4040 _Jv_RegisterClasses
   4060 _ITM_registerTMCloneTable
   4086 pam_start
   4096 pam_strerror
   4109 pam_chauthtok
   4123 pam_end
   4131 libpam_misc.so.0
   4148 misc_conv
   4158 libaudit.so.1
   4172 _fini
   4178 _init
   4184 libselinux.so.1
   4200 is_selinux_enabled
   4219 security_getenforce
   4239 context_user_get
   4256 security_compute_av
   4276 matchpathcon
   4289 freecon
   4297 context_free
--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@debian8:/tmp# strings -t d -n 15 /usr/bin/passwd | more
    568 /lib64/ld-linux-x86-64.so.2
   3997 _ITM_deregisterTMCloneTable
   4040 _Jv_RegisterClasses
   4060 _ITM_registerTMCloneTable
   4131 libpam_misc.so.0
   4184 libselinux.so.1
   4200 is_selinux_enabled
   4219 security_getenforce
   4239 context_user_get
   4256 security_compute_av
   4532 __stack_chk_fail
   4693 __errno_location
   5150 __libc_start_main
   5246 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 accoun
t
  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:

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

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

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@debian8:/tmp# comm -12 /etc/passwd /root/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
comm: file 1 is not in sorted order
comm: file 2 is not in sorted order
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
systemd-timesync:x:100:103:systemd Time Synchronization,,,:/run/systemd:/bin/false
systemd-network:x:101:104:systemd Network Management,,,:/run/systemd/netif:/bin/false
systemd-resolve:x:102:105:systemd Resolver,,,:/run/systemd/resolve:/bin/false
systemd-bus-proxy:x:103:106:systemd Bus Proxy,,,:/run/systemd:/bin/false
usbmux:x:116:46:usbmux daemon,,,:/var/lib/usbmux:/bin/false
lightdm:x:117:124:Light Display Manager:/var/lib/lightdm:/bin/false
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@debian8:/tmp# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin

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

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

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

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

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

root@debian8:/tmp# head -c -150 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
systemd-timesync:x:100:103:systemd Time Synchronization,,,:/run/systemd:/bin/false
systemd-network:x:101:104:systemd Network Management,,,:/run/systemd/netif:/bin/false
systemd-resolve:x:102:105:systemd Resolver,,,:/run/systemd/resolve:/bin/false
systemd-bus-proxy:x:103:106:systemd Bus Proxy,,,:/run/systemd:/bin/false
trainee:x:1000:1000:trainee,,,:/home/trainee:/bin/bash
sshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin
Debian-exim:x:105:110::/var/spool/exim4:/bin/false
messagebus:x:106:111::/var/run/dbus:/bin/false
statd:x:107:65534::/var/lib/nfs:/bin/false
avahi-autoipd:x:108:113:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false
avahi:x:109:115:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
colord:x:110:117:colord colour management daemon,,,:/var/lib/colord:/bin/false
dnsmasq:x:111:65534:dnsmasq,,,:/var/lib/misc:/bin/false
speech-dispatcher:x:112:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh
pulse:x:113:119:PulseAudio daemon,,,:/var/run/pulse:/bin/false
rtkit:x:114:121:RealtimeKit,,,:/proc:/bin/false 

Both x and y can accept multipliers:

root@debian8:/tmp# head -c 1b /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nroot@debian8:/tmp# 
root@debian8:/tmp# 
root@debian8:/tmp# head -c 512 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nroot@debian8:/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@debian8:/tmp# tail /etc/passwd
avahi-autoipd:x:108:113:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false
avahi:x:109:115:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
colord:x:110:117:colord colour management daemon,,,:/var/lib/colord:/bin/false
dnsmasq:x:111:65534:dnsmasq,,,:/var/lib/misc:/bin/false
speech-dispatcher:x:112:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh
pulse:x:113:119:PulseAudio daemon,,,:/var/run/pulse:/bin/false
rtkit:x:114:121:RealtimeKit,,,:/proc:/bin/false
saned:x:115:122::/var/lib/saned:/bin/false
usbmux:x:116:46:usbmux daemon,,,:/var/lib/usbmux:/bin/false
lightdm:x:117:124:Light Display Manager:/var/lib/lightdm:/bin/false

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

root@debian8:/tmp# tail -n 15 /etc/passwd
trainee:x:1000:1000:trainee,,,:/home/trainee:/bin/bash
sshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin
Debian-exim:x:105:110::/var/spool/exim4:/bin/false
messagebus:x:106:111::/var/run/dbus:/bin/false
statd:x:107:65534::/var/lib/nfs:/bin/false
avahi-autoipd:x:108:113:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false
avahi:x:109:115:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
colord:x:110:117:colord colour management daemon,,,:/var/lib/colord:/bin/false
dnsmasq:x:111:65534:dnsmasq,,,:/var/lib/misc:/bin/false
speech-dispatcher:x:112:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh
pulse:x:113:119:PulseAudio daemon,,,:/var/run/pulse:/bin/false
rtkit:x:114:121:RealtimeKit,,,:/proc:/bin/false
saned:x:115:122::/var/lib/saned:/bin/false
usbmux:x:116:46:usbmux daemon,,,:/var/lib/usbmux:/bin/false
lightdm:x:117:124:Light Display Manager:/var/lib/lightdm:/bin/false

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

root@debian8:/tmp# tail -c 150 /etc/passwd
/lib/saned:/bin/false
usbmux:x:116:46:usbmux daemon,,,:/var/lib/usbmux:/bin/false
lightdm:x:117:124:Light Display Manager:/var/lib/lightdm:/bin/false

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

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

Both x and y can accept multipliers:

root@debian8:/tmp# tail -c 1b /etc/passwd
:/bin/false
colord:x:110:117:colord colour management daemon,,,:/var/lib/colord:/bin/false
dnsmasq:x:111:65534:dnsmasq,,,:/var/lib/misc:/bin/false
speech-dispatcher:x:112:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh
pulse:x:113:119:PulseAudio daemon,,,:/var/run/pulse:/bin/false
rtkit:x:114:121:RealtimeKit,,,:/proc:/bin/false
saned:x:115:122::/var/lib/saned:/bin/false
usbmux:x:116:46:usbmux daemon,,,:/var/lib/usbmux:/bin/false
lightdm:x:117:124:Light Display Manager:/var/lib/lightdm:/bin/false
root@debian8:/tmp# 
root@debian8:/tmp# tail -c 512 /etc/passwd
:/bin/false
colord:x:110:117:colord colour management daemon,,,:/var/lib/colord:/bin/false
dnsmasq:x:111:65534:dnsmasq,,,:/var/lib/misc:/bin/false
speech-dispatcher:x:112:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh
pulse:x:113:119:PulseAudio daemon,,,:/var/run/pulse:/bin/false
rtkit:x:114:121:RealtimeKit,,,:/proc:/bin/false
saned:x:115:122::/var/lib/saned:/bin/false
usbmux:x:116:46:usbmux daemon,,,:/var/lib/usbmux:/bin/false
lightdm:x:117:124:Light Display Manager:/var/lib/lightdm:/bin/false

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@debian8:/tmp# tail -f /var/log/messages
Nov 30 16:19:14 debian8 kernel: [182064.453711] usb 2-1: new full-speed USB device number 4 using ohci-pci
Nov 30 16:19:14 debian8 kernel: [182064.716207] usb 2-1: New USB device found, idVendor=80ee, idProduct=0021
Nov 30 16:19:14 debian8 kernel: [182064.716213] usb 2-1: New USB device strings: Mfr=1, Product=3, SerialNumber=0
Nov 30 16:19:14 debian8 kernel: [182064.716215] usb 2-1: Product: USB Tablet
Nov 30 16:19:14 debian8 kernel: [182064.716217] usb 2-1: Manufacturer: VirtualBox
Nov 30 16:19:14 debian8 kernel: [182064.732384] input: VirtualBox USB Tablet as /devices/pci0000:00/0000:00:06.0/usb2/2-1/2-1:1.0/0003:80EE:0021.0003/input/input9
Nov 30 16:19:14 debian8 mtp-probe: checking bus 2, device 4: "/sys/devices/pci0000:00/0000:00:06.0/usb2/2-1"
Nov 30 16:19:14 debian8 mtp-probe: bus: 2, device: 4 was not an MTP device
Nov 30 16:19:14 debian8 kernel: [182064.734594] hid-generic 0003:80EE:0021.0003: input,hidraw0: USB HID v1.10 Mouse [VirtualBox USB Tablet] on usb-0000:00:06.0-1/input0
Nov 30 16:19:20 debian8 kernel: [182070.401997] e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
^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@debian8:/tmp# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 08:00:27:c4:c9:56  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fec4:c956/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:68601 errors:0 dropped:0 overruns:0 frame:0
          TX packets:140516 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:8392325 (8.0 MiB)  TX bytes:10525989 (10.0 MiB)

root@debian8:/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:fec4:c956/64 Scope:Link

root@debian8:/tmp# ifconfig eth0 | grep "inet" | grep -v "inet6"
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0

root@debian8:/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

root@debian8:/tmp# ifconfig eth0 | grep "inet" | grep -v "inet6" | tr -s " " ":" | cut -d: -f4
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 © 2011-2019 Hugh Norris.<br><br>

</html>


Menu