Learn everything about the head and tail command in Linux with codes and examples.
The head and tail commands in Linux are used to quickly inspect a file. The head command inspects the heading of a file where the tail inspects the tail of it. They both have almost similar options and work almost similarly. In this tutorial, you’ll learn everything you need to know to get the best use of Linux head and tail commands.
Before You Start
To practice the head and tail command, create some files that you can easily track what’s going on. I’ve created multiple simple files. The most important one contains 50 lines of text with a line number.
If you don’t know how to create them, read these previous blogs:
Linux head command with example
The head command in Linux or Unix or Bash inspects the head of a file. By default, it displays the top 10 lines of the file you’re inspecting. But you can always choose how many lines to print and how many files to inspect at once!
The basic syntax for the head command:
$ head [option] path/to/file
Say you want to inspect the head of a file, named lines.txt, that contains 50 lines. Here’s how you can do it:
$ head lines.txt
In Linux Mint, it looks like:
You can inspect multiple files at once. Just separate the file names by white space. The top 10 lines from each file will be displayed serially. Let’s see it in action.
$ head dictionary.txt file1.txt file2.txt
- The ls command lists all the files and folders on the current working directory.
A few things to note here:
- You can inspect any document file, not just text files.
- If the file does not have enough data, then both the head and tail commands will display the entire file.
- When inspecting multiple files, by default, you can see the file name displayed on top of the result.
Options in the Linux head command
The head command does not have any shortcuts, but it has some options to use when displaying the file. To see all available options, use any of the following commands:
$ man head $ head --h # or --help
Here’s the result:
Print specific number of lines
By default, with the head command, you can inspect the top 10 lines of the file. But you can specify how many lines you want to see. The syntax for doing this:
$ head -n NUM path/to/file $ head --lines=NUM path/to/file # any of these 2
Here -n is the option, and NUM is the number of lines you want to see. To display top 5 lines:
$ head -n 5 lines.txt
An alternative to this is to directly use NUM in place of n. But make sure to include the dash sign before it.
$ head -5 lines.txt
Notice one thing here. You can use the head command to do the exact opposite of its basic usage. I mean, if you use -NUM instead of NUM, then the minus sign will tell the head command NOT to print the LAST NUM lines, and print everything before it. So the next command will print lines 1 to 45 in a 50 lines file.
$ head -n -5 lines.txt
The output should look like this:
Print specific number of bytes
You can limit the number of bytes to show for each file. One byte represents one character. Remember, the new line character also counts as a byte. This option works the same as the previous one. Use any of these syntax:
$ head -c NUM path/to/file $ head --bytes=NUM path/to/file
Here NUM represents how many bytes to show. You can also use -NUM to show all bytes, but not the last NUM bytes from the file. Try out these command one by one:
$ head -c 10 file1.txt file2.txt # first 10 bytes of each $ head -c 10 file1.txt -c 20 file2.txt # "-c 20" will overwrite "-c 10" $ head -c -10 file1.txt # will skip last 10 bytes but will print all others
- When reading multiple files at once, if one file does not exist, then that part will raise an error. But it won’t stop the process for any file before or after it.
In both of the above options, you can multiply the NUM with some suffix which shortens the text. That means, instead of writing 1024 as NUM, you can use 1k. All the available suffix and their meanings are:
|Others||T, P, E, Z, Y|
Did you download the dictionary.txt file in the previous blog? If not, see ‘before you start’ section here.
Let’s print the first and last 1024 bytes from the dictionary.txt file. And then first and last 1024 lines!
$ head -c K dictionary.txt $ head -c -K dictionary.txt $ head -n 3K dictionary.txt $ head -n -3K dictionary.txt
Notice how fast it’s working. But if you had to do it from a GUI software, then it would take a little bit longer time. That’s the power of bash!
Omit printing file names
When inspecting multiple files, did you notice that every file showed its file name above the result? Sometimes it might get awkward to see. To omit printing file names as headers, use -q or –quite or –silent options, which says “never print headers giving file names”. Use any of these, all are the same.
$ head -n 3 -q lines.txt dictionary.txt $ head -q -n 3 lines.txt dictionary.txt $ head -qn 3 lines.txt dictionary.txt # multiple options at once $ head -nq 3 lines.txt dictionary.txt # This one won't work # because -n looks for NUM just after it
Here’s how it should look like:
Always print file names
Sometimes you might want to print the file names as headers but for some reason, it’s not showing by default, or you just want to be sure that it works. In that case, use the -v or –verbose option. It’s just the opposite of the previous option.
$ head -v -n 3 lines.txt dictionary.txt
head with NUL delimiter
You saw head is printing the top 10 lines by default. How does it recognize a line?
At the end of each line a file, there’s a newline character, \n. The head command uses this as a delimiter to recognize a line break and thus prints the top 10 lines. But with the -z or –zero-terminated option, an ASCII NUL will be considered as a delimiter. See details in the official documentation.
Linux tail command with example
The tail command in Linux, as you can guess, is a complementary command of the head. But it’s more important than the head command and is used so frequently that it has more options than the head command. The tail command prints out the last part of the given files. By default, it prints out the last 10 lines of each file you give it. The best use of it to inspect log files in real-time, where new entries are added as time goes. Hence you can inspect what’s going on in your log files.
The basic syntax of the tail command is as follows:
$ tail [option] path/to/file
To inspect the last part of the lines.txt file:
$ tail lines.txt
And you can inspect multiple files at once:
$ tail lines.txt dictionary.txt file3.doc
Options in the Linux tail command
To view all the available options in the tail command, open the manual or the help display, which are common for every command. They usually print the same window but can be different as you’ve seen in the previous blog.
$ man tail $ tail --help $ tail --h
It should look like this:
You can see that the tail command has lots of similar options as the head command. So I’ll cover them shortly, and try to explain the new options.
Limit number of lines
You can limit the number of lines to show with the tail command. To see the last 5 lines:
$ tail -n NUM path/to/file # syntax 1 $ tail -NUM path/to/file # syntax 2 $ tail -n 5 lines.txt # example
The tail command with -n option has a unique feature. You can choose from what line to show.
If you use -NUM instead of NUM, then all the lines starting from NUM line will be printed. So to print lines 30 to the end:
$ tail -n -30 lines.txt
It should look like this:
Limit number of bytes
You can limit how many bytes to show. It’s the same as before. To see the last 2000 bytes of the dictionary.txt file:
$ tail -c NUM path/to/file # syntax 1 $ tail --bytes=NUM path/to/file # syntax 2 $ tail -c 2KB dictionary.txt # example
When you use -NUM, it will print from NUM bytes to the end of the file.
To print from 2000 bytes to the end of the dictionary.txt file:
$ tail -c -2KB dictionary.txt
Real-time view of a file
The -f or –follow option is used to inspect a file as it’s being changed. When you want to watch a log file or any file that gets new entries over time, you can use this option. To get the best of this option, try reading the official documentation of the Linux tail command.
$ tail -f /var/log/auth.log
Remember, this command does not end. When you’re done, interrupt the command to exit.
Go to the /var/log directory and list down all the log files. Then choose which file to inspect. If you don’t know how to do that, read navigating directories with ls and cd.
Retry opening a file
Use the –retry option if a file seems to be inaccessible. It keeps trying to open the file.
$ tail --retry path/to/file
Examples of Using the head and tail command
* Printing a range of lines with Linux head and tail command
For this section, you need to be familiar with bash pipes.
Say you want to print the line 21 to 30 from the 50 lines text file.
Grab the top 30 lines with the head command. Then pass it to the tail command using the pipe. Now use tail to grab the last 10 lines from it.
$ head -n 30 lines.txt | tail -n 10
This is how it should look like:
Conclusion and Further Reading
If you’re new to Linux or not following all the tutorials as described in the Bash Scripting page, then try reading these blogs: