|
The Unix head and tail commands are very similar. The head command prints lines from the beginning of a file, and the tail command prints lines from the end of files.
The head command
By default the head command prints the first ten lines of a file:
head file1
If you want to print more or less than 10 lines from the beginning of the file, the -n option lets you specify how many lines you want to see. Here I specify that I only want five lines:
head -n 5 file1
and here I say that I want to see 25 lines:
head -n 25 file1
The tail command
By default the tail command also prints ten lines of a file, but it prints the last 10 lines:
tail file1
Like the head command, the tail command also lets you specify a number other than 10 using the -n option:
tail -25 file1
The tail command has another very powerful option: the -f option prints from the end of the file, but also keeps the file open, and keeps printing from the tail of the file as the file itself grows. This is great for looking at the end of a log file. You can see new lines that are added to the log file, as they are added, like this:
tail -f apache.log
That's an extremely powerful feature that I use often for watching files as they grow (mostly log files of one sort or the other).
Related links
|