OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

head / tail – Print First / Last Part Of Files

Sometimes you don't want all the output from a command. You may only want the first few lines or the last few lines. The head command prints the first ten lines of a file and the tail command prints the last ten lines. By default, both commands print ten lines of text, but this can be adjusted with the “-n” option:



[me@linuxbox ~]$ head -n 5 ls-output.txt

total 343496

-rwxr-xr-x 1 root root 31316 2007-12-05 08:58 [

-rwxr-xr-x 1 root root 8240 2007-12-09 13:39 411toppm

-rwxr-xr-x 1 root root 111276 2007-11-26 14:27 a2p

-rwxr-xr-x 1 root root 25368 2006-10-06 20:16 a52dec [me@linuxbox ~]$ tail -n 5 ls-output.txt

-rwxr-xr-x 1 root root 5234 2007-06-27 10:56 znew

-rwxr-xr-x 1 root root 691 2005-09-10 04:21 zonetab2pot.py

-rw-r--r-- 1 root root 930 2007-11-01 12:23 zonetab2pot.pyc

-rw-r--r-- 1 root root 930 2007-11-01 12:23 zonetab2pot.pyo

lrwxrwxrwx 1 root root 6 2016-01-31 05:22 zsoelim -> soelim

[me@linuxbox ~]$ head -n 5 ls-output.txt

total 343496

-rwxr-xr-x 1 root root 31316 2007-12-05 08:58 [

-rwxr-xr-x 1 root root 8240 2007-12-09 13:39 411toppm

-rwxr-xr-x 1 root root 111276 2007-11-26 14:27 a2p

-rwxr-xr-x 1 root root 25368 2006-10-06 20:16 a52dec [me@linuxbox ~]$ tail -n 5 ls-output.txt

-rwxr-xr-x 1 root root 5234 2007-06-27 10:56 znew

-rwxr-xr-x 1 root root 691 2005-09-10 04:21 zonetab2pot.py

-rw-r--r-- 1 root root 930 2007-11-01 12:23 zonetab2pot.pyc

-rw-r--r-- 1 root root 930 2007-11-01 12:23 zonetab2pot.pyo

lrwxrwxrwx 1 root root 6 2016-01-31 05:22 zsoelim -> soelim


These can be used in pipelines as well:



[me@linuxbox ~]$ ls /usr/bin | tail -n 5

[me@linuxbox ~]$ ls /usr/bin | tail -n 5


znew zonetab2pot.py zonetab2pot.pyc zonetab2pot.pyo zsoelim

znew zonetab2pot.py zonetab2pot.pyc zonetab2pot.pyo zsoelim


tail has an option which allows you to view files in real-time. This is useful for watch- ing the progress of log files as they are being written. In the following example, we will look at the messages file in /var/log (or the /var/log/syslog file if mes- sages is missing). Superuser privileges are required to do this on some Linux distribu- tions, since the /var/log/messages file may contain security information:


[me@linuxbox ~]$ tail -f /var/log/messages

Feb 8 13:40:05 twin4 dhclient: DHCPACK from 192.168.1.1

Feb 8 13:40:05 twin4 dhclient: bound to 192.168.1.4 -- renewal in 1652 seconds.

Feb 8 13:55:32 twin4 mountd[3953]: /var/NFSv4/musicbox exported to both 192.168.1.0/24 and twin7.localdomain in 192.168.1.0/24,twin7.localdomain

Feb 8 14:07:37 twin4 dhclient: DHCPREQUEST on eth0 to 192.168.1.1 port 67

Feb 8 14:07:37 twin4 dhclient: DHCPACK from 192.168.1.1

Feb 8 14:07:37 twin4 dhclient: bound to 192.168.1.4 -- renewal in 1771 seconds.

Feb 8 14:09:56 twin4 smartd[3468]: Device: /dev/hda, SMART Prefailure Attribute: 8 Seek_Time_Performance changed from 237 to 236 Feb 8 14:10:37 twin4 mountd[3953]: /var/NFSv4/musicbox exported to both 192.168.1.0/24 and twin7.localdomain in 192.168.1.0/24,twin7.localdomain

Feb 8 14:25:07 twin4 sshd(pam_unix)[29234]: session opened for user me by (uid=0)

Feb 8 14:25:36 twin4 su(pam_unix)[29279]: session opened for user root by me(uid=500)

[me@linuxbox ~]$ tail -f /var/log/messages

Feb 8 13:40:05 twin4 dhclient: DHCPACK from 192.168.1.1

Feb 8 13:40:05 twin4 dhclient: bound to 192.168.1.4 -- renewal in 1652 seconds.

Feb 8 13:55:32 twin4 mountd[3953]: /var/NFSv4/musicbox exported to both 192.168.1.0/24 and twin7.localdomain in 192.168.1.0/24,twin7.localdomain

Feb 8 14:07:37 twin4 dhclient: DHCPREQUEST on eth0 to 192.168.1.1 port 67

Feb 8 14:07:37 twin4 dhclient: DHCPACK from 192.168.1.1

Feb 8 14:07:37 twin4 dhclient: bound to 192.168.1.4 -- renewal in 1771 seconds.

Feb 8 14:09:56 twin4 smartd[3468]: Device: /dev/hda, SMART Prefailure Attribute: 8 Seek_Time_Performance changed from 237 to 236 Feb 8 14:10:37 twin4 mountd[3953]: /var/NFSv4/musicbox exported to both 192.168.1.0/24 and twin7.localdomain in 192.168.1.0/24,twin7.localdomain

Feb 8 14:25:07 twin4 sshd(pam_unix)[29234]: session opened for user me by (uid=0)

Feb 8 14:25:36 twin4 su(pam_unix)[29279]: session opened for user root by me(uid=500)


Using the “-f” option, tail continues to monitor the file and when new lines are ap- pended, they immediately appear on the display. This continues until you type Ctrl-c.


Top OS Cloud Computing at OnWorks: