Difference Between Head and Tail Commands in Linux

In the Linux operating system, head and tail are fundamental command-line utilities used for viewing parts of text files or piped data streams. While both commands are designed to inspect file contents without loading entire documents into memory, their fundamental difference lies in where they read: head outputs the beginning of a file, whereas tail outputs the end. This article explains the technical differences, syntax, common options, and best use cases for both commands.

The Core Difference

Key Features and Common Options

Both commands share similar syntax and common flags, but each has unique capabilities tailored to its purpose.

Specifying Line Count (-n)

You can override the default 10-line limit on either command using the -n option:

Specifying Byte Count (-c)

Both utilities can read files by data size rather than lines using the -c flag:

Real-Time File Monitoring (tail -f)

The most significant operational difference is tail's ability to follow files in real time. The -f (follow) option keeps the file open and prints new lines as they are appended to the file:

The head command does not possess a follow option because beginning lines in a file do not change dynamically during normal logging operations.

When to Use Each Command