How to Use the ‘head’ Command in Linux
Get a quick glimpse of file beginnings with the Linux 'head' command.
The head
command stands as a simple yet powerful tool for file manipulation. It’s primarily used to display the first few lines of a file, allowing users to quickly glance at the beginning of a document without needing to open the entire file. This can be incredibly handy for previewing large files or scripts.
Similar to the head
command, the tail
command is often used in conjunction, allowing users to view the end of a file. Together, these commands provide a comprehensive view of a file’s content. The head
command is particularly useful for system administrators, data analysts, and developers who need to sift through log files or large datasets. It can also be paired with other commands like grep
to filter specific information
How to Use head
1. head
Explanation: Displays the first 10 lines of a file.
Example: head file.txt
Output:
Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10
The command displays the first 10 lines of the file named file.txt
.
2. head -n 5
Explanation: Displays the first 5 lines of a file.
Example: head -n 5 file.txt
Output:
Line 1 Line 2 Line 3 Line 4 Line 5
The command displays the first 5 lines of the file named file.txt
.
3. head -c 20
Explanation: Displays the first 20 bytes of a file.
Example: head -c 20 file.txt
Output:
Line 1 Line 2 Li
The command displays the first 20 bytes of the file named file.txt
.
4. head -q
Explanation: Displays the first 10 lines of multiple files without headers.
Example: head -q file1.txt file2.txt
Output:
Line 1 of file1 Line 2 of file1 ... Line 1 of file2 Line 2 of file2 ...
The command displays the first 10 lines of both file1.txt
and file2.txt
without printing the file names as headers.
5. head -v
Explanation: Displays the first 10 lines of multiple files with headers.
Example: head -v file1.txt file2.txt
Output:
==> file1.txt <== Line 1 of file1 ... ==> file2.txt <== Line 1 of file2 ...
The command displays the first 10 lines of both file1.txt
and file2.txt
, including the file names as headers.
6. head -n -5
Explanation: Displays all but the last 5 lines of a file.
Example: head -n -5 file.txt
Output:
Line 1 ... Line (n-5)
The command displays all the lines of file.txt
except the last 5.
7. tail file.txt | head -n 3
Explanation: Displays the first 3 lines of the last 10 lines of a file.
Example: tail file.txt | head -n 3
Output:
Line (n-9) Line (n-8) Line (n-7)
The command first retrieves the last 10 lines of file.txt
using tail, then pipes that output to head, displaying the first 3 lines of those last 10.
8. head -n 5 file1.txt file2.txt
Explanation: Displays the first 5 lines of multiple files.
Example: head -n 5 file1.txt file2.txt
Output:
==> file1.txt <== Line 1 of file1 ... ==> file2.txt <== Line 1 of file2 ...
The command displays the first 5 lines of both file1.txt
and file2.txt
, including the file names as headers.
More Linux commands:
Directory Operations | rmdir · cd · pwd · exa · ls |
File Operations | cat · cp · dd · less · touch · ln · rename · more · head |
File System Operations | chown · mkfs · locate |
Networking | ping · curl · wget · iptables · mtr |
Search and Text Processing | find · grep · sed · whatis · ripgrep · fd · tldr |
System Information and Management | env · history · top · who · htop · glances · lsof |
User and Session Management | screen · su · sudo · open |