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.

What Is the head Command on Linux?

terminal opened on a laptop running Ubuntu

Hannah Stryker / How-To Geek

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.

version of head command displayed on the terminal

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.

head command help displayed on the terminal

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.

example text file contents are displayed on the terminal using the cat command

you’re able to use theheadcommand inconjunction with other commandsfor selective, real-time monitoring.

It also accepts some options that modify its behavior and output.

If no file is specified, theheadcommand reads from thestandard input.

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

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.

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

-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.

head command that displays all entries of the text file excluding the last two

-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.

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

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.

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

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.

This will skip the last N lines of a file.

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

This is helpful when you want to skip some lines at the end of a file.

Consider you have the same file example.txt, and it contains 15 lines of text.

A negative number can also be defined with the-coption.

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

This will display all bytes of a file, except the last N bytes.

For this, you have to pipe theheadcommand with other text processing commands likecut,awk, orsed.

Use the pipe operator (|) to pipe bothheadandawkcommands.

head command with -v option that displays the file content with the file name

In this way, the output of theheadcommand will serve as input to theawkcommand.

However, it can display the file name when used with multiple files.

Use the-voption to get the file name along with its content.

head command with multiple files as an argument

This option prints a header with the filename of the specified file.

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.

head command that displays two files output without header name on terminal

you could use theheadcommand with the-qoption to view the content of multiple files without displaying their names.

Using head With Other Commands

Theheadcommand can also be used with other commands to perform various tasks.

it’s possible for you to use it withtail,more,wc, andgrepcommands.

head command piped with grep command

you could 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’ve got the option to also pipe theheadcommand with thewccommand.

head command piped with wc command

Both these commands will output the count of total lines, words, and bytes in the 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.

head command piped with the tail command

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.

It can also monitor a file and display each new text entry to that file as they occur.

Just like theheadcommand, you could 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.