Head - view the top of files
Head is simple tool to view the top of files. In its simplest form you can use it to see the first ten lines of a file.
1 2 3 4 5 6 7 8 9 10 11 | |
By default it outputs the first ten lines of a file but you can modify that by passing the -n option
1 2 | |
In fact you don’t even need to use -n. You can just use a number.
1 2 | |
This is useful for viewing comments and the the top of files in general but becomes really useful when you pipe things together.
1 2 3 4 5 6 7 | |
Here we are searching the contents of zip files with a search string but we only want the first occurence. So we can use head to filter the results to just one.
Tail
Tail is the opposite to head but operates on the end of files.
1 2 3 4 5 6 7 8 9 10 11 | |
It also accepts the -n argument for the number of lines you want
1 2 | |
This also works passing just a number
1 2 | |
Watching log files
Tail has the option to watch files in real time so you can keep an eye on log files. You can enable this by passing the -f option.
1
| |
This is really useful for watching logs. You can use it for watching apache, mail logs - any file!
head and tail are simple tools but are extemely useful for everyday usage, especially in piping so worth understanding!