Tips Linux Explorers   All Things Linux Forum   Great Linux Links   LinuxClues.com   Hometown   Email 



HEAD and TAIL


We already know that for reading text files ( config and logs ) we can use the command "cat" or for longer files print them in pages in the console with "less" and "more".
Now imagine you just want to see the first 10 or last 20 lines of a file. Or even follow what is written to a log file "live". For this we use the commands "head" and "tail"

A few examples:
I want to see the last 20 lines of /var/log/boot.log:

CODE
# tail -n 20 /var/log/boot.log


Or the first 30 lines of /etc/sensors.conf:

CODE
# head -n 30 /etc/sensors.conf


The default is 10 lines so you only need the -n option if you want to see more then 10 lines.

Now something a bit more interesting and fun . . . . imagine you want to follow what is been written "live" to the /var/log/messages file ( one of the most important log-files ):

CODE
# tail -f /var/log/messages


This will first show the last 10 lines of the file but it will grow as new lines are added . . . . ( just open another console and log in as root and you will see that that action gets logged by /var/log/messages )

When you get tired of looking at the logfile you can stop the printing of the messages to the terminal with Ctrl+C and it will then return your prompt.



Bruno


-- May 3 2005 ( Revised Dec 10 2005 ) --


Tips Linux Explorers   All Things Linux Forum   Great Linux Links   LinuxClues.com   Hometown   Email