head / tail

What is "head" and "tail"?

  • The simplest definition of the command-line tool head would be to display the first X number of lines in the file.

  • The command-line tool tail displays the last X number of lines in the file.

( X can represent any number)

How to use "head" and "tail"

# syntax : the syntax of the head and tail commands 
head <OPTIONS> <FILE>
tail <OPTIONS> <FILE>

##### how to use head 
head /etc/passwd # displays the first 10 lines of a file
head -n 15 /etc/passwd # displays the first 15 lines of a file
head -c 100 /etc/passwd # print the first 100 characters from the file /etc/passwd

##### how to use tail
tail /etc/passwd # displays the last 10 lines of a file
tail -n 15 /etc/passwd # displays the last 15 lines of a file
tail -c 100 /etc/passwd # print the first character of a file, starting from the bottom line
tail -f /var/log/syslog # follows a file bottom lines as it grows

##### how to combine head an tail with pipe

cat /etc/passwd | head -n 20 | tail -n 10 # first gets the top 20 lines, then from these 20 lines it gets the bottom 10 lines. After that, 

Last updated