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
Description | Head Command |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | – |
Internet Required | No |
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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.