head: the first lines
By default head prints the first ten lines of a file. The option -n changes the count, so head -n 3 prints three lines. The option -c counts bytes instead of lines, which is useful for looking at the start of a binary file or for checking a fixed-width format. When you give several files, head prints a header line with each name, so you can tell the outputs apart.
For a large file, head is cheap. It reads only enough of the file to produce the requested lines and then exits. A program that writes a very large amount of output to a pipe that head closes will receive a broken pipe signal and stop, which is the intended behavior. This is why yes | head -n 5 finishes at once even though yes would run forever.
cd /workspace
seq 1 100 > numbers.txt
head -n 3 numbers.txtThe output is the numbers one, two, and three, each on its own line.
tail: the last lines, and the lines after a point
tail -n 2 prints the last two lines. The option -n with a plus sign changes the meaning. tail -n +98 prints from line 98 to the end, which is the form to use when you know where a section begins. The difference between the two is important: a minus sign counts from the end, and a plus sign counts from the beginning.
To find the end of a file, tail seeks backward from the end of the file in blocks until it has found enough newlines. It does not read the whole file for a line count, so it is fast on large files. For bytes, tail -c works the same way. The one case that requires a full read is the plus form on a large file, because the starting point is counted from the front.
cd /workspace
tail -n 2 numbers.txt
tail -n +98 numbers.txtThe first command prints 99 and 100. The second prints 98, 99, and 100.
tail -f: following a file as it grows
The option -f tells tail to keep the file open after printing the last lines, and to print new lines as they are appended. It is the standard way to watch a log while a service writes to it. The program stays running until you stop it with Ctrl-C, and it reads only what has been added since the last check.
There is a related option, -F, which is more robust. It follows the name rather than the file descriptor, so it continues to work when a log is rotated: the old file is renamed, a new file is created with the same name, and -F notices and switches to the new file. With -f the program would keep reading the renamed file and show nothing new. For logs that rotate, use -F.
tail -f keeps a file open. On a system with many log files, many followers can use file descriptors and clutter the terminal. Stop the follower with Ctrl-C when you finish, and do not leave one running in the background without a reason.
Combining the three
A common workflow inspects a log from the start, then from the end, and then follows it. Use head to check the format and the first timestamp, use tail -n 200 to see recent activity, and use tail -F to watch what happens next. Each step reads less of the file than reading it all, and each step answers a different question. The same tools also combine with grep and wc in pipelines, as the rest of the module shows.
Be careful with pipelines that end in tail -f. The command runs until it is interrupted, so a script that waits for its end will never finish. A script that needs a fixed amount of output should use a count with -n, and only an interactive session should use -f.
Why line counts can mislead
A line is the text between two newline characters. A file that does not end with a newline has a last line that tools may count differently. The output of tail keeps that partial line as it is. Likewise, a file with Windows line endings has the carriage return at the end of each line, which is part of the line for tail. Those details do not usually matter, but they are the reason two tools can disagree on the number of lines in the same file.
Bytes, binary data and pipes
The options -c for head and tail count bytes, not lines. That is the right choice for a binary file, where a line is not a meaningful unit. For example, head -c 16 file.gz | od -An -tx1 shows the first sixteen bytes of an archive as hexadecimal, which is a quick way to see its header. Because the count is in bytes, the output may end in the middle of a line, and that is correct.
Both tools also read from standard input when you give no file name, which lets them sit in the middle of a pipeline. A common pattern is to take the first lines of a command output, such as ps aux | head -n 5, to see a sample without running the whole listing through a viewer. The pipeline stops early because head exits, and the producing program receives a signal when it writes to the closed pipe.
When head is given more than one file, the header lines make the output easy to read, but a script that parses the output should use a single file and a single count. Keep the commands simple, and let each tool do one job.
Practice in the lab
The lab creates a file of one hundred numbers and asks you to take the first lines, the last lines, and a range that starts in the middle. Each result goes into a file so that the checker can compare it exactly. The follow mode is an interactive feature, so the lab uses the options that finish on their own.