head command tutorial in linux/unix with examples and use cases

Linux head command – output the first part of files.

Print the first 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name.

syntax

head [OPTION]... [FILE]...

options

  • -c, –bytes=[-]NUM
    print the first NUM bytes of each file; with the leading ‘-‘, print all but the last NUM bytes of each file
  • -n, –lines=[-]NUM
    print the first NUM lines instead of the first 10; with the leading ‘-‘, print all but the last NUM lines of each file
  • -q, –quiet, –silent
    never print headers giving file names
  • -v, –verbose
    always print headers giving file names
  • -z, –zero-terminated
    line delimiter is NUL, not newline
  • –help
    display this help and exit
  • –version
    output version information and exit

examples

Print top 10 lines of file

➜  ~ head /etc/passwd

Print file header specifies the number of lines – head -n

➜  ~ head -n 3 /etc/passwd

Print multiple file information

➜  ~ head -v /etc/passwd /etc/group

OR

➜  ~ head -v -n 10 /etc/passwd /etc/group

head offset – print 2 to 10 lines of the file

➜  ~ tail +2 /etc/passwd | head -9

linux head skip first line

Use the tail command (tail +n), from line 2 to the end of the file

➜  ~ tail +2 /etc/passwd 

Add a Comment

Your email address will not be published.