head

2024-05-23

Understanding the head Command

The head command displays the beginning of a file, showing the first 10 lines by default. This is useful for quickly inspecting a file’s contents without loading the entire file into memory, especially for large files. It’s a fundamental command for any Linux user, from beginners to seasoned system administrators.

Basic Usage

The simplest use case involves specifying the file name:

head myfile.txt

This command displays the first 10 lines of myfile.txt. If myfile.txt doesn’t exist, you’ll receive an error message.

Let’s create a sample file for demonstration:

echo "Line 1" > myfile.txt
echo "Line 2" >> myfile.txt
echo "Line 3" >> myfile.txt
echo "Line 4" >> myfile.txt
echo "Line 5" >> myfile.txt
echo "Line 6" >> myfile.txt
echo "Line 7" >> myfile.txt
echo "Line 8" >> myfile.txt
echo "Line 9" >> myfile.txt
echo "Line 10" >> myfile.txt
echo "Line 11" >> myfile.txt
echo "Line 12" >> myfile.txt

Now, running head myfile.txt will output:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10

Specifying the Number of Lines

You can control the number of lines displayed using the -n option (or --lines). For example, to display only the first 5 lines:

head -n 5 myfile.txt

This will output:

Line 1
Line 2
Line 3
Line 4
Line 5

You can also use a negative number to display lines from the end of the file. For example head -n -5 myfile.txt will show the last five lines. Note that this behavior might differ slightly between different versions of head.

Handling Multiple Files

The head command can handle multiple files simultaneously. If you provide multiple file names, the output will be prefixed with the filename for each:

head myfile.txt anotherfile.txt

This will display the first 10 lines of myfile.txt, followed by the first 10 lines of anotherfile.txt, each section labeled with the filename.

Piping with Other Commands

The power of head is amplified when combined with other commands using pipes (|). For example, to view the first 5 lines of the output of a grep command:

grep "Line" myfile.txt | head -n 5

This will find all lines containing “Line” in myfile.txt and then display only the first 5 matches.

Bytes Instead of Lines

The -c option (or --bytes) allows you to specify the number of bytes to display instead of lines:

head -c 20 myfile.txt

This displays the first 20 bytes of myfile.txt. This is particularly useful for binary files where line breaks aren’t meaningful.

Using head with Standard Input

head can also read from standard input, which is particularly useful when combined with other commands.

ls -l | head -n 5

This will list all files and directories, and then display only the first five lines of the output.