$_ CLI In Depth
Learning progress 0%
lesson text viewing editors

head, tail & tail -f

6 min read Module 3: Viewing Files & Text Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:08

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.

BASH3 lines
cd /workspace
seq 1 100 > numbers.txt
head -n 3 numbers.txt

The 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.

BASH3 lines
cd /workspace
tail -n 2 numbers.txt
tail -n +98 numbers.txt

The 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.

Warning

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.

How wc counts lines, words and bytes

Visualizer

wc makes one pass over the bytes. A line is a newline character, a word is a run of non-blank characters, and bytes are not characters.

1 / 5
h
i
␠
t
h
e
r
e
\n
o
k
\n
lines
0
words
0
bytes
0

Twelve bytes in total. wc starts with three counters at zero and an "inside a word" flag set to false.

printf "hi there\nok\n" | wc
Terminal lab · 3 steps · real Linux in your browser

Take the start and end of a file

Read the first lines, the last lines, and a range that starts in the middle.

01
Take the first lines
Save the first three lines of numbers.txt to first.txt: ``bash head -n 3 numbers.txt > first.txt ``
Show hint for step 1
head -n sets how many lines to print.
02
Take the last lines
Save the last two lines to last.txt: ``bash tail -n 2 numbers.txt > last.txt ``
Show hint for step 2
tail -n with a plain number counts from the end.
03
Start from a line
Save everything from line 98 to the end: ``bash tail -n +98 numbers.txt > from98.txt ``
Show hint for step 3
A plus sign before the number counts from the beginning of the file.
Check yourself Question 1 of 4
Concept Check What head prints recall

What does head -n 3 print for a file of one hundred numbers?

Recap & Key Takeaways

head prints the first lines or bytes and exits, so it is cheap on large files.
tail -n N prints the last N lines, while tail -n +N starts at line N counting from the beginning.
tail -f follows a file as it grows, and -F also follows the name when a log is rotated.
A script that needs a fixed amount of output should use a count rather than a follow mode.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue