Linux Head Command: View Top of Files & Command Output

Linux TLDR
Last Updated:
Reading time: 3 minutes

In Linux, the head command is a commonly used command-line utility that allows you to view the top portion of a large file or the output of the command.

The head command can be useful in various scenarios, such as taking a quick glimpse at the beginning of a file or command output without loading the entire content into memory.

Large files, such as log files, configuration files, scripts, and outputs from commands like ls, find, ps, history, pstree, etc., deliver massive results; here you can use the head command.

In this guide, you will learn the usage of the head command in Linux with real life examples.

Tutorial Details

DescriptionHead Command
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisites
Internet RequiredNo

Syntax of the Head Command

The head command takes two arguments: one is the option and another is the filename.

$ head [options] [filename]

Here, “filename” serves as an optional argument that allows you to specify the path of the target file you wish to view.

If left unspecified, the head command will instead read from the standard input—perfect for piping the output of another command into it.

Now, let’s delve into a few compelling examples showcasing the usage of the head command.

Sample Text Files

To start, I’ll use the following two sample text files to demonstrate the examples of the head command:

$ cat file1.txt
$ cat file2.txt 

Output:

Content of the sample text files

1. Display the First 10 Lines of a File

The head command without any options will display the 10 lines from the beginning of the file:

$ head file1.txt

Output:

Displaying the 10 lines from the text file

2. Display the First 6 Lines of a File

The “-n N” option allows you to achieve the output of the file for a specific number of lines, in this case, 6.

$ head -n 6 file1.txt

Output:

Displaying the six lines from the text file

3. Display the First 60 Bytes of a File

Every character or integer in the file is precisely one byte in size. To view the file’s content up to a particular number of bytes, simply utilize the “-c N” option.

$ head -c 60 file1.txt

Output:

Displaying the 60-byte content from the text file

4. Display the First 6 Lines from Multiple Files

Let’s say you want to display the initial six lines from two distinct files. Just separate each file name with a space when entering the command.

$ head -n 6 file1.txt file2.txt

Output:

Displaying the first six lines from two different files

5. Disable the Display Header

In the previous command, the file name’s header was displayed, which can be annoying. To remove it, simply use the “-q” flag.

$ head -q -n 6 file1.txt file2.txt

Output:

Displaying the first six lines from two different files without a header

6. Sort the Output

Unsorted output can be easily sorted by redirecting the head command output to the sort command:

$ head -q -n 6 file1.txt file2.txt | sort

Output:

Sorting the content of two files

7. Reverse-sort the Output

To effortlessly showcase the results from descending to ascending, simply redirect the head command output to “sort -r“, whereas the “-r” option is used to reverse the results.

$ head -q -n 6 file1.txt file2.txt | sort -r

Output:

Reverse sorting the content of two files

8. View the First 5 Processes of the Ps Command

In Linux, some commands, let’s say “ps -aux” are used to print the current running program and display the massive result on the screen.

To limit the output to a specific number of lines, simply pipe or redirect the output to the head command, which will deliver a restricted number of results.

$ ps -aux | head -n 5

Output:

Displaying the first 5 running processes from the Ps command

9. View the First 5 Processes of the Specific User in the Ps Command

In the preceding command, you could view all processes running for different users. Now, to narrow down the results and focus only on the first 5 processes running under the user (let’s say “linuxtl+“), the command would be:

$ ps -aux | grep "linuxtl+" | head -n 5

Output:

Display the first 5 running processes for a specific user

10. View the First 5 Processes of the Ps Command Except the Specified User

To display all processes running under different users except the root user, the command would look like this:

$ ps -aux | grep -v "root" | head -n 5

Output:

Display the first 5 running processes except for specific user

Final Word

The head command is a very basic but useful Linux command-line utility. I hope the above examples will give you insight into how this command works.

If you have any questions or queries related to this article, feel free to tell us in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.