Quick Links
Summary
On Linux, there arevarious commandsto get the entire contents of a text file. But what if you need a certain part or a few starting lines? The head command prints the initial content of a file on the terminal.
Linux has multiple commands to display file contents. The most popular and frequently used arecat,less, andviewcommands. However, these commands are more useful for displaying large portions of files. If you only need to show a specific number of lines of a file, theheadcommand is there for you.

Theheadcommand does the opposite of what thetailcommand does. It shows the starting content of a file, while thetailcommand prints the ending lines of a file. By default,headdisplays the first 10 lines. If you want to print more or less than 10 lines, just use the-noption. Similarly, the-coption with theheadcommand can restrict the output to a particular byte number.
Theheadcommand cananalyze logsand other text files that are subject to vary over time. You can use theheadcommand inconjunction with other commandsfor selective, real-time monitoring.

Theheadcommand’s syntax is easy to understand and is similar to other Linux commands:
Theheadcommand can take one or multiple files as inputs. It also accepts some options that modify its behavior and output. If no file is specified, theheadcommand reads from thestandard input.

Similarly, type the below command to output theheadcommand help menu:
You can use various options with theheadcommand on Linux. Each option has a concise and extended form, to use with basic syntax. It controls how much data theheadcommand prints to the normal output. For example, it allows you to decide whether to include the header in the file output or not.
The following table contains the list of options available for theheadcommand:

-nor–lines
-cor–bytes

Shows the specified number of bytes from the start.
-vor–verbose
Prints each file name along with the file contents. This is useful when displaying multiple files at once.

-qor–quiet
Suppresses the printing of file names before displaying their contents. This is useful when displaying only one file or when piping the output to another command.

-zor–zero-terminated
Replace the newline character with NULL at the end of each line.

Before moving to the demonstration of theheadcommand, let’s first look at the content of the sample file. Use thecatcommand followed by the filename to get all the data of the sample file.
This example file contains 15 lines of text.
Now, let’s print the content of the example.txt file using theheadcommand. Theheadcommand, without any options, will print the first 10 lines of the file.
Getting a Specific Number of Lines
To display a certain number of lines using theheadcommand, add the-n(–lines) option followed by the file name.
To display the first four lines of example.txt, run:
Similarly, a negative number can also be specified with the-noption. This will skip the last N lines of a file. This is helpful when you want to skip some lines at the end of a file.
For example, to skip the last two lines of the example.txt file, run:

Pulling Specific Number of Bytes Using head
Another use case for theheadcommand is to get a certain number of bytes from the start of a file. you may do this by using the-c(–bytes) option followed by a digit.
Consider you have the same file example.txt, and it contains 15 lines of text. To get the first 20 bytes, run:

As the example.txt file contains ASCII characters, each of the characters including the space and a newline will take one byte.
A negative number can also be defined with the-coption. This will display all bytes of a file, except the last N bytes. To display all the bytes in example.txt, except the last 13 bytes, run:
Viewing Specific Characters in a File
Sometimes, you need to see a certain part of a file, rather than the entire file contents. Let’s say you have a file with multiple lines of contents, and you want to see the first or last characters of each line. For this, you have to pipe theheadcommand with other text processing commands likecut,awk, orsed.
For example, to retrieve the first letter of each line of example.txt, use thecutcommand with the-coption followed by character position. By default, you will get the starting character of the first 10 lines, unless you specify the number of output lines.
To see the last word of each line of example.txt, use theawkcommand with the{print $NF}pattern. Use the pipe operator (|) to pipe bothheadandawkcommands. In this way, the output of theheadcommand will serve as input to theawkcommand.
When you use{print $NF}, it tellsawkto print the value of the last field for each line in the input. By using$NF, you don’t need to know in advance how many fields each line has;awkautomatically handles it for you and extracts the last field.
Seeing the Header/File Name With head
By default, when theheadcommand is used with a single file, it does not print the file name. However, it can display the file name when used with multiple files.
Use the-voption to get the file name along with its content. This option prints a header with the filename of the specified file.
Displaying Contents of Multiple Files With head
Theheadcommand can also take multiple file names as arguments and display their contents in order. Let’s take two files called example.txt and test.txt that contain multiple lines of content. Now, theheadcommand will display both file names along with their content.
you may use theheadcommand with the-qoption to view the content of multiple files without displaying their names.
Theheadcommand can also be used with other commands to perform various tasks. You can use it withtail,more,wc, andgrepcommands.
You can pipe theheadcommand withgrepto give you all the lines that contain the specified pattern.
The above syntax displays all lines in the example.txt file that contain “ch”.
You can also pipe theheadcommand with thewccommand. Both these commands will output the count of total lines, words, and bytes in the file.
To get the number of lines, words, and bytes in the example.txt file, run:
You can use theheadandtailcommands together with the pipe symbol to display a specific range of lines from a file. Theheadcommand shows the starting lines of a file, while thetailcommand prints the ending lines of a file.
Consider the example.txt file that contains 15 lines. To display the contents between 5th and 11th lines, run:
This command works by first using thehead -n 10command to show the first 10 lines of the file. After that, it will pipe the output to thetail -n 5command. Thetailcommand will give us the final output of entities that are between the 5th and 11th lines.
Want to Display Line Endings With head?
Theheadcommand, as its name implies, is primarily concerned with the initial lines of a file. Conversely, thetailcommand serves the purpose of displaying the concluding lines of a text file. Usually, new data is added to the end of a file, so thetailcommand is a quick and easy way to see the most recent additions to a file. It can also monitor a file and display each new text entry to that file as they occur.
Just like theheadcommand, you can also usetailto monitor multiple files or count the number of bytes. It can also check a specific pattern or text inclusion in the text file. This makes it a great tool to monitor log files.