head: Display the first few lines of a file
August 9th, 2024 9:31 AM Mr. Q Categories: Command
Command: head
Used to display the first few lines of a file. By default, it shows the first 10 lines.
Sample Command and Output:
$ head file1.txt
This is the first line of file1.txt.
This is the second line.
This is the third line.
This is the fourth line.
This is the fifth line.
This is the sixth line.
This is the seventh line.
This is the eighth line.
This is the ninth line.
This is the tenth line.
Description:
head file1.txt
: Displays the first 10 lines offile1.txt
. This is useful for quickly viewing the beginning of a file.
Sample Command and Output:
$ head -n 5 file1.txt
This is the first line of file1.txt.
This is the second line.
This is the third line.
This is the fourth line.
This is the fifth line.
Description:
head -n 5 file1.txt
: Displays the first 5 lines offile1.txt
. The-n
option allows specifying the number of lines to display.
Sample Command and Output:
$ head -c 20 file1.txt
This is the first 20 bytes of
Description:
head -c 20 file1.txt
: Displays the first 20 bytes offile1.txt
. The-c
option allows specifying the number of bytes to display instead of lines.
Additional Arguments:
- -q: Suppresses the output of file headers when multiple files are listed.
- -v: Always outputs the file headers, even when only one file is being displayed.
Sample Command and Output:
$ head -q file1.txt file2.txt
==> file1.txt <==
This is the first line of file1.txt.
...
==> file2.txt <==
This is the first line of file2.txt.
...
Description:
head -q file1.txt file2.txt
: Displays the first 10 lines of bothfile1.txt
andfile2.txt
without showing file headers for each file. The-q
option suppresses file names in the output.