This article will show you how to use the head command in Linux. head is used to print out the beginning of a file. For example, this is useful when you have a giant text file and you want to preview the contents without attempting to open the entire file. Here are some common examples on the usage:
Table of Contents
Using ‘head’ in Ubuntu, CentOS, Fedora, and RHEL
1. Displaying the First N Characters from a File
You can use the -c
(bytes) option with head
to display the first N characters from a file:
head -c 50 filename
In this example, head
will output the first 50 characters from the file named “filename”.
2. Combining head
with tail
to Display Specific Lines
You can combine head
and tail
commands to display specific lines from a file. For example, to display lines 10 to 20 of a file:
head -20 filename | tail -11
Here, head -20 filename
will first get the first 20 lines of the file, and tail -11
will then get the last 11 lines from that output (which are lines 10 to 20).
3. Using head
with Pipes and Grep
You can use head
in combination with other commands like grep
using pipes. For instance, to find a specific word in a file and display the first ten instances:
grep 'word' filename | head
In this case, grep
will find lines containing ‘word’ in the file, and head
will display the first ten of those lines.
4. Displaying Multiple Files
head
can also output the beginning lines of multiple files. When used with multiple files, head
will also display the file names:
head file1 file2 file3
This command will display the first ten lines of file1, file2, and file3, preceded by the respective file name.
5. Changing the Amount of Output
The default behavior of head
is to display the first ten lines of a file. However, you can specify a different number of lines using -n
option:
head -n 15 filename
In this case, head
will output the first 15 lines of “filename”.
Examples Using Both ‘head’ and ‘grep’ Together
In Linux/Unix grep, short for “global regular expression print”, is a command used in searching and matching text files contained in the regular expressions. We’ll show you some more advanced use-cases when using both head and grep together.
Sure, the head
and grep
commands are powerful tools when used together. Here are some advanced examples:
1. Find the First File containing a Specific Word in a Directory
grep -rl "search_word" /path/to/directory | head -n 1
This command will return the first file that contains “search_word” within the specified directory.
2. Extract First N Lines containing a Specific Word from a File
grep "search_word" filename | head -n 10
This will find the first ten lines containing “search_word” from the file.
3. Display the First File that Contains a Certain Pattern in a Large Set of Files
grep -rl "pattern" * | head -n 1
This command searches for the specified “pattern” across all files in the current directory and displays the first file that contains the pattern.
4. Find the First N Occurrences of a Pattern across Multiple Files
grep -r "pattern" /path/to/directory | head -n 10
This will output the first ten lines containing the “pattern” from all files in the specified directory.
5. Display a Specific Number of Log Entries containing a Certain String
grep "Error 404" /var/log/apache2/access.log | head -n 20
This command will display the first 20 log entries containing “Error 404” from the Apache access log file.
Frequently Asked Questions (FAQ)
What is the maximum file size the ‘head’ command supports?
The head
command in Linux, including distributions like Ubuntu, CentOS, and RHEL, does not have a specified maximum file size limit. It’s capable of handling files as large as the file system allows. The command reads the file sequentially from the start, so it’s not impacted by the overall file size but rather by the amount of data it needs to read, which is usually quite small.
What is the difference between head and tail in Linux?
The head
and tail
commands in Linux are used to display the beginning and ending of files respectively. The head
command outputs the first part (typically the first 10 lines) of one or more files, while the tail
command shows the last part (by default, the last 10 lines) of files. Both commands are especially useful for viewing large files where viewing the entire content at once can be overwhelming.