Skip to main content

Mastering the `head` and `tail` Commands in Linux

The head and tail commands in Linux are indispensable utilities for viewing the beginning and end of files, respectively. These commands come in handy when you're dealing with large text files and need a quick glance at the content without opening the entire file. In this article, we will explore the syntax, options, and practical use-cases for both head and tail.

Syntax

head [OPTION]... [FILE]...

tail

tail [OPTION]... [FILE]...
  • OPTION: Flags that modify the behavior of the command.
  • FILE: The file(s) to read. If this parameter is omitted, both commands read from standard input.

Options for head and tail

Here is a table outlining commonly used options for head and tail:

CommandOptionShorthandDescription
head--lines=NUM-n NUMOutput the first NUM lines instead of the default first 10 lines
tail--lines=NUM-n NUMOutput the last NUM lines instead of the default last 10 lines
tail--follow-fOutput appended data as the file grows
tail--pid=PID-sWith -f, terminate after process ID PID dies
Both--quiet-qNever output headers giving file names
Both--verbose-vAlways output headers giving file names
Both--helpDisplay a help message and exit

Practical Examples

Create example.txt file
Line 1: This is the first line.
Line 2: This is the second line.
Line 3: This is the third line.
Line 4: This is the fourth line.
Line 5: This is the fifth line.
Line 6: This is the sixth line.
Line 7: This is the seventh line.
Line 8: This is the eighth line.
Line 9: This is the ninth line.
Line 10: This is the tenth line.
Line 11: This is the eleventh line.
Line 12: This is the twelfth line.
Line 13: This is the thirteenth line.
Line 14: This is the fourteenth line.
Line 15: This is the fifteenth line.
Line 16: This is the sixteenth line.
Line 17: This is the seventeenth line.
Line 18: This is the eighteenth line.
Line 19: This is the nineteenth line.
Line 20: This is the twentieth line.
Line 21: This is the twenty-first line.
Line 22: This is the twenty-second line.
Line 23: This is the twenty-third line.
Line 24: This is the twenty-fourth line.
Line 25: This is the twenty-fifth line.

head

Basic Usage

To display the first 10 lines of a file named example.txt:

head example.txt

Custom Line Count

To display the first 20 lines:

head -n 20 example.txt

or

head --lines=20 example.txt

tail

Basic Usage

To display the last 10 lines of a file named example.txt:

tail example.txt

Custom Line Count

To display the last 20 lines:

tail -n 20 example.txt

or

tail --lines=20 example.txt

Follow Mode

To follow the changes in a file in real-time:

tail -f example.txt

Terminate with PID

To terminate the tail -f command after a specific process ID (PID) terminates:

tail -f --pid=1234 example.txt

Combining Multiple Options

You can combine multiple options for more tailored output. For example, to display the first 20 lines of multiple files and include headers:

head -n 20 -v file1.txt file2.txt

Using head and tail Together

You can use head and tail together to extract specific line ranges from a file. For example, to display lines 20 to 30 from a file:

head -n 30 example.txt | tail -n 11

Piping with Other Commands

Both head and tail work well with pipes. For example, to count the number of files in a directory:

ls | head -n 10

Conclusion

The head and tail commands are invaluable when dealing with text files on a Linux system. They offer a variety of options for line count, real-time following, and even conditional termination. Whether you're tailing logs or reviewing configuration files, mastering these commands will significantly improve your efficiency when working with text files.

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle