Advanced head
Command Options
While the basic usage of the head
command is straightforward, it also supports several advanced options that can be used to customize its behavior. These options can be particularly useful when working with complex file structures or when you need to perform more advanced tasks.
Displaying a Specific Number of Lines
By default, the head
command displays the first 10 lines of a file. However, you can specify a different number of lines to display using the -n
option. For example, to display the first 5 lines of a file, you can use the following command:
head -n 5 example.txt
Displaying a Specific Number of Bytes
In addition to displaying a specific number of lines, you can also use the head
command to display a specific number of bytes. This can be useful when working with binary files or when you need to preview the beginning of a file without displaying the entire contents. To display the first 100 bytes of a file, you can use the following command:
head -c 100 example.txt
Displaying Multiple Files
The head
command can also be used to display the first few lines of multiple files at once. To do this, simply specify the file names as arguments to the head
command. For example, to display the first 10 lines of both file1.txt
and file2.txt
, you can use the following command:
head file1.txt file2.txt
When displaying the contents of multiple files, the head
command will display a header for each file. If you don't want to see these headers, you can use the -q
(quiet) option to suppress them. For example:
head -q file1.txt file2.txt
By understanding these advanced options, you can use the head
command more effectively and efficiently when working with files in Linux.