How to Use head Command in Linux

Wondering how to use the head command in Linux? 

Linux consists of various commands that are headed by Bash. Bash is the software executing various commands on the Terminal. It is also referred to as a shell. One of the Ubuntu shell commands is the Bash Head command.

We are already familiar with the flexibility Ubuntu offers its users and developers with the OS. It has various commands to interpret, analyze, display or run different types of files. One of the commands that aid us in displaying the content of a file is the Head command.

Head command is particularly used to display specific parts of a file. To add a layer of accuracy, it is used with its counterpart command: the Bash Tail command, which will display the data from the end of a given file.

So without further ado, let us delve into the working of Head command!


The head command reads a file and prints the selected number of lines appearing at the start of it. It has the general syntax.

$ head <possible options> <file(s)>

Now that you’re aware of the syntax, let’s proceed to learn how to use the head command in Linux.

Display Lines of a Given File.

We can use the Head command to print the first lines of any given file. The command needed a default status in case no options were provided. This standard status is 10 lines. This implies that using the Head command without using any additional options will prompt the system to print the first 10 lines of a given file.

head Command in Linux

Note: In case you face the error “no such file or directory” after you have made a file, make sure you save your file as “filename.txt” (the extension is important). On the Terminal, run this command.

$ cat <filename.txt>


$ head <filename.txt>

And you will have the content of your file. Also, make sure your directory in the Terminal is the one you have saved your file in.

The head command can be manipulated to print any given number of lines as required by the user. This can be done by using the “-n” option. This will tell the head command to pint the first n number of lines as shown.

head Command in Linux

The general syntax is.

$ head -n <NUMBER> <filename.txt>

It should be noted that it is necessary to put the number of lines we need to be displayed when using the -n option. This way, the Terminal can effectively implement the command without giving an error. However, we may choose to omit -n as shown:

$ head -20 <filename.txt>

Display Specified Number of Bytes.

We can also operate the Head command to display a specified number of bytes from our file using the -c or --bytes option. This option enables you to print the bytes as specified by the number written in the command.

If you choose to write 250 after c, the command will print the first 250 bytes from your file. Every character (char) occupies a byte, and the number of bytes dictates the characters to be displayed. This option has more to do with the content rather than the number of lines.

To print a specified number of bytes, type.

$ head -c <NUMBER> filename.txt
display the number of bytes

Or we can use suffixes as stated below.

  • multiply by 512: b 
  • Multiply by 1000: kb 
  • Multiply by 1024: K
  • Multiply by 1000000: mb
  • Multiply by 1048576: M

And so on, from Giga to Tera to Exa, the line continues the same way.

head Command in Linux

We can print the given file content without displaying the title using the -q or --quiet option.

$ head -q <filename>
head Command in Linux

Or without the q option,

without the q option

As we can see, the titles are not displayed with the -q option enabled. 

To print one or more files using the head command, we type.

$ head -n3 <filename.txt> <filename2.txt>

Which will print the first 3 lines of both file1 and file2. The header will show the sequence in which multiple files are displayed.

head Command in Linux

Verbose Option.

-v or --verbose is an option best used when we need a briefing on our file content. Using -v, our file’s content is displayed, preceded by the file name.

$ head -v <filename.txt>
head Command in Linux

As stated previously, we can use the -q option to display content without header/filename.


The --version option enables us to check our head command version. For that, we simply type.

$ head --version
head Command in Linux

Use with Other Commands. 

Head command can be used alongside various other commands like tee, echo, tail, etc. Usage with these commands opens up brand new applications which are open to exploring for developers and beginners alike. For instance, you may also print from K to L lines in a file using head and tail commands in an adjacent manner.

$ head -n -10 list.txt | tail -n -10

This command enables the tail to omit the last 10 lines in a given file. These are referred to by the head command and displayed as shown.

head Command in Linux
key locations

File Filtering Using grep.

Using head in conjunction with commands like Grep can enable us to find and filter certain keywords, phrases, numbers, etc. the Head is used with Grep via pipeline to display results as shown. 

$ head <file> | grep <search term>
head Command in Linux

Here the output of the head file is displayed and sent to pipe (|), redirecting its output as input to the grep command, which performs its filtering and displays output as shown above.

Head command is one of the base commands that can be used to handle files effectively. It is a nifty tool that can prove quite useful when used along with other commands. You can check its description by heading to its manpage or typing the following command:

$ man head
head Command in Linux

We hope this guide helped you learn how to use the head command in Linux. If you wish to know about other useful commands like the rmmod command and how it works, click here.

If you have any questions or queries regarding the guide, let us know in the comment section down below.

If this guide helped you, please share it. 🙂

Leave a Reply

Your email address will not be published.

Previous Post

How to Use the alias command in Linux

Next Post

How to Fix FPS Drops on Fortnite

Related Posts