When you need to view the first few lines of a file, the Linux head command is a simple solution.
The default behavior of the command is to print the first 10 lines of the target file. However, you can change this behavior with the option we will discuss later. In addition, you can combine the head command with pipes to use it with standard input or other commands.
This makes the head command an essential part of the sysadmin and developer toolkit.
In this tutorial, we will take a close look at the head command. In addition to the basic syntax, we will discuss six use cases where the command solves real-world challenges.
Table Of Contents
- The Basic Syntax of the head Command in Linux
- Create the Test File & Test Run the head Command
- 6 Ways of Using the head Command in Linux
- The Prerequisites
- Application #1: Adjust the Number of Displayed Lines
- Application #2: Specify the Number of Bytes to Display
- Application #3: Display the Filename Tag in the Output
- Application #4: View the Initial Lines from Multiple Files
- Application #5: Redirecting the Output to a File
- Application #6: Combine head with Pipelines
- Conclusion
- FAQs
The Basic Syntax of the head Command in Linux
The basic syntax of the head command is as follows:
# head [option] file_name
This command can be executed with or without optional arguments, depending on your requirements.
Options for the head Command
Options for the head command enable you to adjust the standard output, specify the exact lines of data you wish to view. Most of these options are available in both a short and a long format, either of which can be appended to the standard command structure.
Here’s a list of the most common options you can use with the head command:
Also Read: 13 Examples That Showcase the xargs Command in Linux
Create the Test File & Test Run the head Command
Let’s create a test file named countries.txt. This file will contain a list of 14 countries, with each name on a separate line.
You can use your preferred text editor to create this file. We will use the following command to create this file in Vim:
# sudo vim countries.txt
Next, enter the following entries into the file:
India
United States
France
Germany
Italy
United Kingdom
Spain
Brazil
Russia
Indonesia
Nigeria
Russia
China
Mexico
Save the file and exit the editor.
Now, verify the contents of the file by using the cat command to display the file in the terminal:
# cat countries.txt
Now that you have the test file ready and verified, let’s try the head command with the following:
# head countries.txt
This command will display the first 10 lines from the file, as shown in this screenshot:
6 Ways of Using the head Command in Linux
Now that you know the syntax and the basic operation of the head command, let’s see 6 applications of the command.
The Prerequisites
Before trying out the ideas we will mention later on, make sure you have the following:
- A system running Ubuntu or any other mainstream Linux distribution
- A user account with root or sudo privileges
- A text file for trying out the head command
Application #1: Adjust the Number of Displayed Lines
The head command, by default, shows the initial 10 lines of a file.
You can alter the number of lines shown in the output using the -n (or –lines) option followed by the desired number of lines and the file name. The syntax of the command in this case will be:
# head -n [number] file_name
For instance, if you want to view only the first 5 lines of the countries.txt, you would execute:
# head -n 5 countries.txt
Application #2: Specify the Number of Bytes to Display
In addition to lines, the head command allows you to specify the output in terms of bytes. This is done using the -c (or –bytes) option. The syntax of the command in this case is as follows:
# head -c [number] file_name
So, to display the first 20 bytes from the countries.txt file, the command would be:
# head -c 20 countries.txt
Do you know that the Linux tail command is used to display the LAST 10 lines of a file? Check out our detailed Linux tail command tutorial in which we covered several practical examples
Application #3: Display the Filename Tag in the Output
You can include the file name at the beginning of the command’s output with the -v (or –verbose) option. If you are working with multiple files at the same time, the filename tag at the top is a great way of matching the lines with the file in the output:
The syntax of the head command, in this case, would be as follows:
# head -v file_name
For instance, use the following command to have the file name displayed along with the first 10 lines from countries.txt:
# head -v countries.txt
Application #4: View the Initial Lines from Multiple Files
The head command is capable of displaying the first few lines from several files in a single invocation. For this, we recommend the following syntax:
# head [option] file_name1 file_name2
So, for instance, to view the first lines from both countries.txt and more_countries.txt, you could use the following command:
# head countries.txt more_countries.txt
This command will show the file names followed by their respective first 10 lines.
You can modify this command to change the number of lines in the output. For instance, the following command will display the first four lines from each file:
# head -n 4 countries.txt more_countries.txt
Application #5: Redirecting the Output to a File
The output generated by the head command can be redirected into a file using the redirection (>) symbol. Usually, this process is used to redirect the output to a text or log file.
In this scenario, the output of the command is saved in the specified file instead of being displayed in the terminal. Note that because of the way redirection works, the contents of the file will be replaced with the new content if the target file already exists. Alternatively, if the file does not exist, the command will create it with the given name.
The format for redirecting output from the head command to a file is as follows:
# head [options] file_name > output_file
For instance, to save the first 10 lines of the countries.txt into a file called output1.txt, the command would be:
# head countries.txt > output.txt
We recommend verifying the contents of the output file to verify the successful redirection of the output. For this, use the cat command to display the contents of the file in the terminal:
# cat output.txt
Application #6: Combine head with Pipelines
The head command can be seamlessly integrated into a pipeline with other commands to further manipulate the output. The syntax in this scenario would be:
# [command] | head [option]
For instance, to list the first ten entries of the contents of the /etc directory, run the following command:
# ls /etc | head
This is just one use case where a pipe connects two commands to help you do more with a single command. In our detailed tutorial on using pipes with popular Linux commands, we have covered more use cases.
Conclusion
Mastering the Linux head command enhances file management in Linux, allowing quick previews of file beginnings directly from the terminal. This guide covered its basic to advanced uses, which are essential for efficient navigation and data management.
In demanding environments, like those using RedSwitches’ bare metal hosting, head proves invaluable for swift file inspections, aiding in maintaining system performance and quick issue resolution.
Ready to optimize your Linux server performance? Upgrade to affordable dedicated hosting solutions from RedSwitches today! With powerful hardware and expert support, RedSwitches ensures your server runs smoothly, allowing you to focus on your projects. Take your hosting to the next level with RedSwitches.
So, if you’re looking for a robust server for your Linux projects, we offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.
FAQs
Q. What is the syntax of the head command in Linux?
The syntax of the head command in Linux is “head [OPTION]… [FILE]…”
Q. How can I use the head command to display the first 20 lines of a text file?
You can use the command “head -20 filename” to display the first 10 lines of the file “file.txt”
Q. What does the head command in Linux do?
The head command in Linux prints the first 10 lines by default to the standard output.
Q. Can I specify the number of lines to be displayed using the head command?
Certainly, the number of lines to display can be defined by using the -n option with the head command, followed by the desired line count. For instance, the command head -n 15 file.txt will reveal the top 15 lines of the file named file.txt.
Q. How do I use the head command on multiple files?
The head command allows for simultaneous operation on several files by listing their names as arguments. For instance, executing head file1.txt file2.txt would show the initial 10 lines from each of the specified files.
Q. What are some common examples of using the head command on Linux?
Some practical examples of using the head command on Linux include displaying the first 5 lines of a file, excluding the last 3 lines, and displaying a specific number of bytes from a file.
Q. How is the head command different from the tail command in Linux?
The head command prints the first 10 lines of a file, while the tail command prints the last 10 lines of a file by default.
Q. Can the head command show a specific number of bytes?
Indeed, by employing the -c option with the head command, you can specify the exact number of bytes to be displayed. For example, using head -c 50 filename.txt will present the first 50 bytes of the specified file.
Q. How can I use the head with pipes command?
The head command can be integrated into a pipeline to handle the output from a preceding command. For example, using ls -l | head -n 3 will display the first 3 entries from the list of files in the current directory.