Or copy link
Copy link
File handling is a day-to-day task of Linux users and administrators. One of the most commonly performed file-handling tasks is accessing the file’s content. This becomes more interesting and complicated when the files contain gigantic data with hundreds and thousands of lines and we need only a few lines from the start. In that case, we can use the Linux head command that fetches the file’s first N lines. This command helps us keep things simple by fetching only the necessary data instead of loading the complete file’s content.
In this tutorial, we will explain the head command and how to use it in Linux to display a file’s content from the top.
In Linux, the commands like cat, head, or tail, are used to display the content of a file. The cat command displays the entire file while the tail command shows the content from the end. However, the head command reads the content from the beginning of the file. By default, it displays the top 10 lines of the selected file.
To use the head command in Linux, specify the head keyword followed by the name of the target file. Additionally, we can specify optional arguments to modify the behavior of the Linux head command according to our requirements:
head [options] fileName
We can run the head command with one of the following optional arguments to modify the output:
Let’s go through the following head command examples to see how it works with or without optional flags. We’ve already created a sample file named sampleFile.txt, and we will use it in all the examples to demonstrate the head command:
sampleFile.txt
cat sampleFile.txt
Read also How to Use the wc Command in Linux
Let’s run the head command without any optional argument to get the top ten lines of the sampleFile.txt:
head sampleFile.txt
In the following code, we use the -n option with the head command to show the specific number of lines in the output:
head -n 7 sampleFile.txt
This time the head command returns the top seven lines of the sampleFile.txt:
You can execute the head command with the -c option to specify the number of bytes in the output:
head -c 14 sampleFile.txt
The output shows the first 14 bytes of the sampleFile.txt, where bytes include characters, spaces, and newlines:
Let’s run the head command with the -v or –verbose option to print N number of lines along with the file name. This option is specifically useful when dealing with multiple files:
head -n 3 -v sampleFile.txt
This command will print the first three lines of the smapleFile.txt along with the file name:
smapleFile.txt
We can use the head command with a space-separated syntax to show the top N lines of multiple files:
head -n 3 sampleFile.txt exampleFile.txt
The output shows that the above command prints the top three lines of both specified files:
From the output, you can observe that each file’s content is separated by the respective file name.
Explore head Command with Ultahost Linux VPS!
Ultahost Linux VPS solutions offer flexibility and control. You can easily practice the head command and other Linux tools with our fast and reliable VPS, which guarantees uptime!
You can use the -q option to print the multiple files’ content without specifying their names in the output:
head -n 3 -q sampleFile.txt exampleFile.txt
In the following output, the first three lines are from sampleFile.txt, while the other three lines are from exampleFile.txt:
exampleFile.txt
We can use the > sign to redirect the output of the head command to a file instead of printing it on the console:
head -n 5 sampleFile.txt > sampleClone.txt
We can verify the output by using the cat command, as follows:
cat sampleClone.txt
We can pipe the output of the head command to one or more commands to modify the output. For example, in the following command, we use the head command with the ls command to display the first few entries of a directory listing:
ls -l | head -n 6
This command will show the first six files and directories in the long format (-l):
This is how the head command works in Linux.
The head command is an essential utility for Linux users and administrators. It allows them to access the file’s content from the start. Moreover, it simplifies file handling by fetching only the necessary lines or bytes, especially when dealing with large files. It can be used with several options to tailor the output according to our needs. In this article, we discussed how to use the head command in different scenarios, including working with multiple files and redirecting output.
The head command enables you to quickly view the top N lines of files. This streamlines your file-handling tasks. To enhance your experience and boost performance, consider Ultahost’s fast VPS hosting. With a range of affordable VPS plans, you can easily find one that suits your needs.
The head command is a command line utility in Linux that shows the top N lines of a file. By default, it shows the first 10 lines, but this can be modified using the -n option.
You can use the -n option to specify the number of lines, the -c option to specify the number of bytes, the -v option to display the file name in the output, and the -q option to hide the file name when showing multiple files.
Yes, you can use the -n option to specify a different number of lines. For example, head -n 5 sampleFile.txt will display the first five lines of the sampleFile.txt.
You can use the -c option followed by the number of bytes. For example, head -c 14 sampleFile.txt will show the first 14 bytes of the file.
You can specify multiple file names in the command separated by a space. For example, head -n 3 example1.txt example2.txt will display the first three lines of both files.
Yes, you can use the > sign to redirect the output. For instance, head -n 5 sampleFile.txt > outputFile.txt will write the first five lines of sampleFile.txt to outputFile.txt.
Yes, you can pipe the output of the head command to other commands. For example, ls -l | head -n 6 will show the first six entries of a directory listing.
The "tar.gz" file format is a common archive format use...
The man command, short for “manual”, is an essentia...
The dmesg command is a useful tool in Linux that lets y...
Environment variables play an important role in shaping...
BitrixVM is a powerful and flexible virtual machine des...
Bash scripts are powerful tools that allow you to autom...
Save my name, email, and website in this browser for the next time I comment.
Δ