Wondering how to use the head command in Linux?
Linux consists of various commands that are headed by Bash. Bash is the software executing various commands on the Terminal. It is also referred to as a shell. One of the Ubuntu shell commands is the Bash Head command.
We are already familiar with the flexibility Ubuntu offers its users and developers with the OS. It has various commands to interpret, analyze, display or run different types of files. One of the commands that aid us in displaying the content of a file is the Head command.
Head command is particularly used to display specific parts of a file. To add a layer of accuracy, it is used with its counterpart command: the Bash Tail command, which will display the data from the end of a given file.
So without further ado, let us delve into the working of Head command!
The head command reads a file and prints the selected number of lines appearing at the start of it. It has the general syntax.
$ head <possible options> <file(s)>
Now that you’re aware of the syntax, let’s proceed to learn how to use the head command in Linux.
Display Lines of a Given File.
We can use the Head command to print the first lines of any given file. The command needed a default status in case no options were provided. This standard status is 10 lines. This implies that using the Head command without using any additional options will prompt the system to print the first 10 lines of a given file.
Note: In case you face the error “no such file or directory” after you have made a file, make sure you save your file as “filename.txt” (the extension is important). On the Terminal, run this command.
$ cat <filename.txt>
$ head <filename.txt>
And you will have the content of your file. Also, make sure your directory in the Terminal is the one you have saved your file in.
Print Select Number of Lines.
The head command can be manipulated to print any given number of lines as required by the user. This can be done by using the “-n” option. This will tell the head command to pint the first n number of lines, as shown.
The general syntax is.
$ head -n <NUMBER> <filename.txt>
It should be noted that it is necessary to put the number of lines we need to be displayed when using the
-n option. This way, the Terminal can effectively implement the command without giving an error. However, we may choose to omit
-n as shown:
$ head -20 <filename.txt>
Display Specified Number of Bytes.
We can also operate the Head command to display a specified number of bytes from our file using the
--bytes option. This option enables you to print the bytes as specified by the number written in the command.
If you choose to write 250 after c, the command will print the first 250 bytes from your file. Every character (char) occupies a byte, and the number of bytes dictates the characters to be displayed. This option has more to do with the content rather than the number of lines.
To print a specified number of bytes, type.
$ head -c <NUMBER> filename.txt
Or we can use suffixes as stated below.
- multiply by 512:
- Multiply by 1000:
- Multiply by 1024:
- Multiply by 1000000:
- Multiply by 1048576:
And so on, from Giga to Tera to Exa, the line continues the same way.
Print File Content Discreetly.
We can print the given file content without displaying the title using the
$ head -q <filename>
Or without the
As we can see, the titles are not displayed with the
-q option enabled.
To print one or more files using the
head command, we type.
$ head -n3 <filename.txt> <filename2.txt>
Which will print the first 3 lines of both file1 and file2. The header will show the sequence in which multiple files are displayed.
--verbose is an option best used when we need a briefing on our file content. Using
-v, our file’s content is displayed, preceded by the file name.
$ head -v <filename.txt>
As stated previously, we can use the
-q option to display content without header/filename.
--version option enables us to check our head command version. For that, we simply type.
$ head --version
Use with Other Commands.
Head command can be used alongside various other commands like
tail, etc. Usage with these commands opens up brand new applications which are open to exploring for developers and beginners alike. For instance, you may also print from K to L lines in a file using head and tail commands in an adjacent manner.
$ head -n -10 list.txt | tail -n -10
This command enables the tail to omit the last 10 lines in a given file. These are referred to by the head command and displayed as shown.
File Filtering Using grep.
Using head in conjunction with commands like Grep can enable us to find and filter certain keywords, phrases, numbers, etc. the Head is used with Grep via pipeline to display results as shown.
$ head <file> | grep <search term>
Here the output of the head file is displayed and sent to pipe (|), redirecting its output as input to the grep command, which performs its filtering and displays output as shown above.
Head command is one of the base commands that can be used to handle files effectively. It is a nifty tool that can prove quite useful when used along with other commands. You can check its description by heading to its manpage or typing the following command:
$ man head
We hope this guide helped you learn how to use the head command in Linux. If you wish to know about other useful commands like the rmmod command and how it works, click here.
If you have any questions or queries regarding the guide, let us know in the comment section down below.
If this guide helped you, please share it. 🙂