head's Examples

to print first 5 number of linnes

head -n 2 numbers.txt

print 4 lines in numbers.txt

head -4 numbers.txt

print all lines but  nor the last 2 lines in the file

head -n -2 numbers.txt

-c option prints the N number of bytes from the initial part of the file

head -c 5 numbers.txt

-c option prints the N number of bytes from the initial part of the file

head -c 5 numbers.txt

pass the output of other commands to the input of the head

ls | head

to print header information always

head -v numbers.txt

displays multiple files

head colors.txt numbers.txt

redirect the head command to a text file

head numbers.txt > output1_file

to list all the files /etc directory using ls command and prints 10 entries 

ls /etc |head

Explanation:
Shows the first 10 lines of filename.txt.

head filename.txt

Explanation:
Shows the first 20 lines of filename.txt.

head -n 20 filename.txt

Explanation:
Shows only the first 5 lines.

head -n 5 filename.txt

Explanation:
Shows the first 10 lines of file1.txt and file2.txt separately.

head file1.txt file2.txt

Explanation:
Displays the first 50 characters (bytes) of filename.txt

head -c 50 filename.txt

Explanation:
Shows the first 15 lines of syslog (useful for troubleshooting logs).

head -n 15 /var/log/syslog

Explanation:
Displays the first 20 lines of filename.txt and filters lines containing "error".

head -n 20 filename.txt | grep "error"

Explanation:
Shows the first 10 processes from ps aux output.

ps aux | head -n 10

Explanation:
Extracts and displays the first 10 lines of file.txt.gz without fully decompressing.

zcat file.txt.gz | head -n 10

Displays the first few lines (default 10) of a file.

head -n 5 /etc/passwd

DevOpsSchool
Typically replies within an hour

DevOpsSchool
Hi there 👋

How can I help you?
×
Chat with Us