How to use the head command in Linux

Key takeaways

  • The Linux head command alone, for example “head example.txt”, will list the first ten lines of a text file or entry.
  • You can customize the head command to display a specific number of lines using the “-n” option, or limit the output to particular bytes using the “-c” option.
  • You can also use the head command for selective real-time system monitoring by analyzing logs and other text files that may change over time.


In Linux, there are different commands to 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 contents of a file to the terminal.


What is head command in Linux?

Linux has several commands for displaying file contents. The most popular and frequently used are cat, lessAnd view orders. However, these commands are more useful for viewing large portions of files. If you only need to display a specific number of lines of a file, the head the order is there for you.

THE head the command does the opposite of what the command tail the command does it. It shows the starting contents of a file, while the tail The command prints the end lines of a file. By default, head displays the first 10 lines. If you want to print more or less than 10 lines, just use the -n option. Likewise, the -c possibility with the head The command can restrict the output to a particular byte number.

THE head The command can analyze logs and other text files that may vary over time. You can use the head command in conjunction with other commands for selective real-time monitoring.

THE head The command syntax is easy to understand and is similar to other Linux commands:

head (OPTION)... (FILE)...

THE head The command can take one or more files as input. It also accepts certain options that modify its behavior and output. If no file is specified, the head The command reads from standard input.

To check the version of the head command, use:

head --version

version of the head command displayed on the terminal

Similarly, type the command below to display the head command help menu:

head --help

head command help displayed on terminal

head command options

You can use various options with the head command under Linux. Each option has a concise and expanded form, to be used with basic syntax. It controls the amount of data head The command prints to normal output. For example, it allows you to decide whether or not to include the header in the file output.

The following table contains the list of options available for the head order:

Option

Description

-n Or --lines

Defines the number of lines to display from the beginning.

-c Or --bytes

Shows the number of bytes specified from the beginning.

-v Or --verbose

Prints each file name with the file contents. This is useful when viewing multiple files at once.

-q Or --quiet

Suppresses printing file names before displaying their contents. This is useful when displaying a single file or passing the output to another command.

-z Or --zero-terminated

Replace the newline character with NULL at the end of each line.

Before moving on to the demonstration of head command, let’s first look at the contents of the example file. Use the cat command followed by the file name to get all the data from the example file.

cat example.txt

an example of text file content is displayed on the terminal using the cat command

This example file contains 15 lines of text.

Now let’s print the contents of the example.txt file using the head order. THE head The command, without any options, will print the first 10 lines of the file.

head example.txt

The head command displays the output of the text file on the terminal

Get a specific number of rows

To display a certain number of lines using the key head command, add the -n (--lines) option followed by the file name.

To view the first four lines of example.txt, run:

head -n 4 example.txt

head command with the -n option showing the first four entries of a text file

Similarly, a negative number can also be specified with the -n option. This will ignore the last N lines of a file. This is useful when you want to skip certain lines at the end of a file.

For example, to ignore the last two lines of the example.txt file, run:

head -n -2 example.txt

head command which displays all entries in the text file except the last two

Extract a specific number of bytes using head

Another use case for head The command is to get a certain number of bytes from the start of a file. You can do this using the -c (--bytes) option followed by a number.

Consider that you have the same example.txt file and it contains 15 lines of text. To get the first 20 bytes, run:

head -c 20 example.txt

head command which displays the first 20 characters of a text file on the terminal

Since the example.txt file contains ASCII characters, each of the characters, including space and a newline, will occupy one byte.

A negative number can also be defined with the -c option. This will show all bytes in a file except the last N bytes. To display all bytes in the example.txt file except the last 13 bytes, run:

head -c -13 example.txt

head command with -c option which excludes the last 13 characters from the text file and displays the output on the terminal

Displaying specific characters in a file

Sometimes you need to see a certain part of a file, rather than its entire contents. Let’s say you have a file with several lines of content and you want to see the first or last character of each line. To do this, you must channel the head command with other word processing commands like cut, awkOr sed.

For example, to retrieve the first letter of each line in the example.txt file, use the command cut order with the -c option followed by the character position. By default, you will get the start character of the first 10 lines unless you specify the number of output lines.

head example.txt | cut -c 1

head command piped with the cut command and displaying the first characters of each line on the terminal

To see the last word of each line in the example.txt file, use the awk order with the {print $NF} model. Use the pipe operator (|) to pipe both head And awk orders. In this way, the output of head the command will serve as input to the awk order.

When you use {print $NF}it tells awk to print the value of the last field for each line of the input. Using $NFyou don’t need to know in advance how many fields each row contains; awk automatically handles it for you and extracts the last field.

head example.txt | awk '{print $NF}'

head command coupled with the awk command to display the second word of each line

By default, when the head The command 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 -v option to get the file name and its contents. This option prints a header with the name of the specified file.

head -v example.txt

head command with -v option which displays the contents of the file with the file name

Showing contents of multiple files with head

THE head The command 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 several lines of content. Now the head The command will display both file names along with their contents.

head example.txt test.txt

head command with multiple files as argument

You can use the head order with the -q option to display the contents of multiple files without displaying their names.

head -q example.txt test.txt

head command that displays two output files without header names on the terminal

Using head with other commands

THE head The command can also be used with other commands to perform various tasks. You can use it with tail, more, wcAnd grep orders.

You can channel the head order with grep to give you all rows containing the specified pattern.

head example.txt | grep ch

head command passed with the grep command

The syntax above displays all lines in the example.txt file that contain “ch”.

You can also channel the head order with the wc order. These two commands will display the total number of lines, words and bytes in the file.

To get the number of lines, words, and bytes in the example.txt file, run:

head example.txt | wc

head command passed with wc command

You can use the head And tail commands along with the pipe symbol to display a specific range of lines in a file. THE head The command displays the beginning lines of a file, while the command tail The command prints the end lines of a file.

Let’s take the example.txt file which contains 15 lines. To display the content between the 5th and 11th lines, run:

head -n 10 example.txt | tail -n 5

head command passed with tail command

This command works by first using the head -n 10 command to display the first 10 lines of the file. After that it will redirect the output to the tail -n 5 order. THE tail The command will give us the final output of the entities located between the 5th and 11th lines.

Do you want to display line endings with the head?

THE head The command, as its name suggests, mainly concerns the first lines of a file. Conversely, the tail The command is used to display the final lines of a text file. Usually, new data is added at the end of a file. tail The command 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 in that file as it appears.

Just like the head command, you can also use tail to monitor multiple files or count the number of bytes. It can also check for a specific pattern or text inclusion in the text file. This makes it a great tool for monitoring log files.

You May Also Like

+ There are no comments

Add yours