14 tail and head commands in Linux/Unix
Let’s start with the tail command, and explore all of the features this handy command can provide and see how to use it best to suit your needs. After that we will show some options that you can do and can not do with the head command. # tail /var/log/messages Mar 20 12:42:22 hameda1d1c dhclient[4334]: DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x280436dd) Mar 20 12:42:24 hameda1d1c avahi-daemon[2027]: Registering new address record for fe80::4639:c4ff:fe53:4908 on eth0.*. Mar 20 12:42:28 hameda1d1c dhclient[4334]: DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x280436dd) Mar 20 12:42:28 hameda1d1c dhclient[4334]: DHCPACK from 10.76.198.1 (xid=0x280436dd) Mar 20 12:42:30 hameda1d1c avahi-daemon[2027]: Joining mDNS multicast group on interface eth0.IPv4 with address 10.76.199.87. Mar 20 12:42:30 hameda1d1c avahi-daemon[2027]: New relevant interface eth0.IPv4 for mDNS. Mar 20 12:42:30 hameda1d1c avahi-daemon[2027]: Registering new address record for 10.76.199.87 on eth0.IPv4. Mar 20 12:42:30 hameda1d1c NET[4385]: /sbin/dhclient-script : updated /etc/resolv.conf Mar 20 12:42:30 hameda1d1c dhclient[4334]: bound to 10.76.199.87 -- renewal in 74685 seconds. Mar 20 12:45:39 hameda1d1c kernel: usb 3-7: USB disconnect, device number 2 Example 2: Now what about you are interested in just the last 3 lines of a file, or maybe interested in the last 15 lines of a file. this is when the -n option comes handy, to choose specific number of lines instead of the default 10. tail -n <number_of_lines> /path/to/file Example : # tail -n 4 /etc/group vboxusers:x:491: avahi:x:70: mailnull:x:47: smmsp:x:51: tail -n <number of lines> <file1> <file2> <file3> Example: surendra@sanne-taggle:~/code/sh$ tail -n 2 99abc.txt startup_script.sh wifiactivate.sh ==> 99abc.txt <== ==> startup_script.sh <== sed -i 's/^.*PermitRootLogin.*$/PermitRootLogin yes/g' /etc/ssh/sshd_config service sshd reload ==> wifiactivate.sh <== modprobe -rv iwlwifi modprobe -v iwlwifi 11n_disable=8 tail -f /path/to/file Example : #service crond start ; tail -f /var/log/cron Starting crond: [ OK ] Mar 20 13:35:01 hameda1d1c CROND[5338]: (root) CMD (/root/mail/mailscript.sh) Mar 20 13:35:33 hameda1d1c crond[2354]: (CRON) INFO (Shutting down) Mar 20 13:35:50 hameda1d1c crond[5385]: (CRON) STARTUP (1.4.4) Mar 20 13:35:50 hameda1d1c crond[5385]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 64% if used.) Mar 20 13:35:51 hameda1d1c crond[5385]: (CRON) INFO (running with inotify support) Mar 20 13:35:51 hameda1d1c crond[5385]: (CRON) INFO (@reboot jobs will be run at computer's startup.) Mar 20 13:36:01 hameda1d1c CROND[5390]: (root) CMD (/root/mail/mailscript.sh) Mar 20 13:36:12 hameda1d1c crond[5385]: (CRON) INFO (Shutting down) Mar 20 13:36:25 hameda1d1c crond[5436]: (CRON) STARTUP (1.4.4) Mar 20 13:36:25 hameda1d1c crond[5436]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 85% if used.) ^C Example 5: The same tail -f command can be replicated using less command well. Once you open a file with less less /path/to/filename Once you open file, then press shift+f Example: Mar 21 08:25:01 sanne-taggle CRON[5553]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1) Mar 21 08:27:24 sanne-taggle wpa_supplicant[807]: wlan0: WPA: Group rekeying completed with da:3c:69:04:b1:21 [GTK=CCMP] Mar 21 08:35:01 sanne-taggle CRON[5982]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1) Waiting for data... (interrupt to abort) Example 6: We have other option -s which should always be used with -f” will determine the sleep interval, whereas tail -f will keep watching the file, the refresh rate is each 1 second, if you wish to control this, then you will have to use the -s option “sleep” and specify the sleep interval tail -f -s <sleep interval in seconds> /path/to/file Example : # tail -f -s 5 /var/log/secure Mar 20 12:43:27 sa su: pam_unix(su:session): session opened for user rabbitmq by (uid=0) Mar 20 12:43:27 sa su: pam_unix(su:session): session closed for user rabbitmq Mar 20 12:43:27 sa su: pam_unix(su:session): session opened for user rabbitmq by (uid=0) Mar 20 12:43:28 sa su: pam_unix(su:session): session closed for user rabbitmq Mar 20 12:43:28 sa su: pam_unix(su:session): session opened for user rabbitmq by (uid=0) tail /path/to/file1 /path/to/file2 Example: # tail -f /var/log/secure /var/log/cron ==> /var/log/secure <== Mar 20 13:13:19 sa su: pam_unix(su:session): session opened for user rabbitmq by (uid=0) Mar 20 13:13:20 sa su: pam_unix(su:session): session closed for user rabbitmq Mar 20 13:13:25 sa su: pam_unix(su:session): session opened for user rabbitmq by (uid=0) Mar 20 13:13:26 sa su: pam_unix(su:session): session closed for user rabbitmq Mar 20 13:13:26 sa su: pam_unix(su:session): session opened for user rabbitmq by (uid=0) Mar 20 13:13:26 sa su: pam_unix(su:session): session closed for user rabbitmq Mar 20 13:13:26 sa su: pam_unix(su:session): session opened for user rabbitmq by (uid=0) Mar 20 13:13:27 sa su: pam_unix(su:session): session closed for user rabbitmq Mar 20 13:13:27 sa su: pam_unix(su:session): session opened for user rabbitmq by (uid=0) Mar 20 13:13:27 sa su: pam_unix(su:session): session closed for user rabbitmq ==> /var/log/cron <== Mar 20 13:00:02 sa CROND[19837]: (root) CMD (/usr/lib64/sa/sa1 1 1) Mar 20 13:01:01 sa CROND[20705]: (root) CMD (run-parts /etc/cron.hourly) Mar 20 13:01:01 sa run-parts(/etc/cron.hourly)[20705]: starting 0anacron Mar 20 13:01:01 sa run-parts(/etc/cron.hourly)[20714]: finished 0anacron Mar 20 13:01:01 sa run-parts(/etc/cron.hourly)[20705]: starting logrotate Mar 20 13:01:01 sa run-parts(/etc/cron.hourly)[20721]: finished logrotate Mar 20 13:02:01 sa CROND[21587]: (root) CMD (/etc/puppet/scripts/update-federation-links.py) Mar 20 13:10:01 sa CROND[28672]: (root) CMD (/usr/lib64/sa/sa1 1 1) Mar 20 13:13:00 sa crontab[31759]: (root) LIST (root) Mar 20 13:13:01 sa CROND[31817]: (root) CMD (/etc/puppet/scripts/update-federation-links.py) ^C Example : # tail -fq /var/log/secure /var/log/cron Mar 20 13:13:19 sa su: pam_unix(su:session): session opened for user rabbitmq by (uid=0) Mar 20 13:13:20 sa su: pam_unix(su:session): session closed for user rabbitmq Mar 20 13:13:25 sa su: pam_unix(su:session): session opened for user rabbitmq by (uid=0) Mar 20 13:13:26 sa su: pam_unix(su:session): session closed for user rabbitmq Mar 20 13:13:26 sa su: pam_unix(su:session): session opened for user rabbitmq by (uid=0) Mar 20 13:13:26 sa su: pam_unix(su:session): session closed for user rabbitmq Mar 20 13:13:26 sa su: pam_unix(su:session): session opened for user rabbitmq by (uid=0) Mar 20 13:13:27 sa su: pam_unix(su:session): session closed for user rabbitmq Mar 20 13:13:27 sa su: pam_unix(su:session): session opened for user rabbitmq by (uid=0) Mar 20 13:13:27 sa su: pam_unix(su:session): session closed for user rabbitmq Mar 20 13:01:01 sa CROND[20705]: (root) CMD (run-parts /etc/cron.hourly) Mar 20 13:01:01 sa run-parts(/etc/cron.hourly)[20705]: starting 0anacron Mar 20 13:01:01 sa run-parts(/etc/cron.hourly)[20714]: finished 0anacron Mar 20 13:01:01 sa run-parts(/etc/cron.hourly)[20705]: starting logrotate Mar 20 13:01:01 sa run-parts(/etc/cron.hourly)[20721]: finished logrotate Mar 20 13:02:01 sa CROND[21587]: (root) CMD (/etc/puppet/scripts/update-federation-links.py) Mar 20 13:10:01 sa CROND[28672]: (root) CMD (/usr/lib64/sa/sa1 1 1) Mar 20 13:13:00 sa crontab[31759]: (root) LIST (root) Mar 20 13:13:01 sa CROND[31817]: (root) CMD (/etc/puppet/scripts/update-federation-links.py) Mar 20 13:20:01 sa CROND[8700]: (root) CMD (/usr/lib64/sa/sa1 1 1) tail -c <number of bytes> /path/to/file Example : # tail -c 500 /var/log/messages failed to connect to device: failed to connect to nws://admin@localhost:56025/?group=Administrators&cert=%2Fvar%2Flib%2Fpuppet%2Fssl%2Fcerts%2Fc569b530-6b4f-4d72-8417-48a556bf55a5.pem&key=%2Fvar%2Flib%2Fpuppet%2Fssl%2Fprivate_keys%2Fc569b530-6b4f-4d72-8417-48a556bf55a5.pem Mar 20 13:27:07 sa collectd[2249]: nwservice.py: Upstart service in status (status:) does not match nwipdbextractor Mar 20 13:27:07 sa collectd[2249]: nwservice.py: Upstart service in status (status:) does not match nwbroker Head command in Linux Head command will obviously on the contrary to tail, it will print the first 10 lines of the file. Till this part of the post, the head command will do pretty much the same as tail in all previous examples, with exception to the -f option, there is no -f option in head, which is very natural since files will always grow from the bottom. head command syntax in Linux head [OPTION]... [FILE]... Example 10: As mention earlier print first 10 lines. # head /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin # head -n 2 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin head -n <number of lines preceeded with "-"> /path/to/file Example : # head -n -27 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync Combine head and tail command in Linux Example 13: As tail and head commands print different parts of files in an effective way, we can combine these two to print some advanced filtering of file content. To print 15th line to 20th line in /etc/passwd file use below example. head -n 20 /etc/passwd | tail -n 5 Output: syslog:x:101:104::/home/syslog:/bin/false messagebus:x:102:106::/var/run/dbus:/bin/false usbmux:x:103:46:usbmux daemon,,,:/home/usbmux:/bin/false dnsmasq:x:104:65534:dnsmasq,,,:/var/lib/misc:/bin/false kernoops:x:106:65534:Kernel Oops Tracking Daemon,,,:/:/bin/false Example: $ sed -n '5p' /etc/passwd sync:x:4:65534:sync:/bin:/bin/sync |
|
This topic does not have any threads posted yet!
You cannot post until you login.