An interesting and useful tool in Linux is the ability to follow along as files are being written. This is especially valuable when the need arises to see log files as their entries are being created. For example, after setting up my procmail filtering system, I wanted to watch the log files being written just to be sure that the system was working as it should. The tool for viewing these “on the fly” log entries is tail
The tail command prints the last ten lines of a named file to the standard output. Using the proper options, tail will repeat the process, printing the new last ten lines. In other words, you can view file creation in a way that’s nearly interactive.
The syntax for the tail command is:
tail [options] [file]
Here are three useful options for tail and the resulting output:
|-f||Follow the file as it grows. Ends when user presses [ctrl][c].|
|-n||Prints the last number of lines.|
|-n[k]||Start printing at the nth k-type item from the end of the file. These items can be lines, blocks, or characters.|
Since I’ve already mentioned ‘tail’ing my procmail log, I’ll show you the command I use by way of example:
[tony@server tony]$ tail -f ./procmail/log &
This command prints the last ten lines of the ./procmail/log file to the console. As new mail is received and processed by procmail, I can watch the console window for the results of that processing, in real time. That’s made possible by the f option, which instructs tail to follow the file as it grows. It’s very useful for testing and debugging the recipes that make up my mail filtering.
Another good use of tail is in combining it with grep. This command will show the last three lines containing instances of the string ‘FORMAIL’ in the .procmailrc file:
[tony@server tony]$ grep ‘FORMAIL’ .procmailrc | tail -3
This can be especially useful when creating and debugging long scripts.
Putting a tail on a file can tell you very useful stuff. It’s like your own personal file detective.