The head
command in Bash is used to display the first few lines of a file or the output of a command. By default, it shows the first 10 lines, but this can be adjusted with options.
The basic syntax of the head
command is as follows:
head [options] [arguments]
-n [number]
: Specify the number of lines to display. For example, -n 5
will show the first 5 lines.-c [number]
: Display the first specified number of bytes instead of lines.-q
: Suppress the output of the file headers when multiple files are being processed.-v
: Always show the file header, even when only one file is being processed.Here are some practical examples of using the head
command:
head filename.txt
head -n 5 filename.txt
head -c 20 filename.txt
head file1.txt file2.txt
head -v filename.txt
head
in combination with other commands using pipes. For example, to see the first 10 lines of a long directory listing:
ls -l | head
head
can quickly give you a preview without opening the entire file.-n
option to suit your needs.