The head
command in Linux is used to display the beginning of a file. By default, it displays the first 10 lines of a file. This command is useful when you want to quickly view the contents of a file without opening it in an editor.
Overview
To use the head
command, open a terminal window and type the following:
head filename
Replace filename
with the name of the file you want to view.
By default, head
will display the first 10 lines of the file. If you want to display a different number of lines, you can use the -n
option followed by the number of lines you want to display. For example, to display the first 5 lines of a file, you would use the following command:
head -n 5 filename
If you want to display the first part of multiple files, you can specify the filenames as arguments to the head
command. For example:
head file1 file2 file3
This will display the first 10 lines of each file.
Options
The following table lists all the available options for the head
command:
Option | Description |
---|---|
-c , --bytes |
Display the first N bytes of the file. |
-n , --lines |
Display the first N lines of the file. |
-q , --quiet |
Do not print headers when displaying multiple files. |
-v , --verbose |
Always print headers when displaying multiple files. |
Troubleshooting tips
If you receive an error message when using the head
command, make sure that you have typed the command correctly and that you have specified the correct filename. If the file does not exist, you will receive an error message.
If you want to display the first part of a file that is very large, the head
command may not be the best option. In this case, you can use the less
command to view the file and navigate through it.
Notes
- The
head
command only displays the beginning of a file. If you want to view the entire contents of a file, you can use thecat
command. - By default, the
head
command displays 10 lines of a file. This can be changed using the-n
option.