head – show first lines of a text file

Screen Shot 2019-01-31 at 17.27.24.png

head command is a great Unix/Linux utility that is super useful when workig with text files. It shows you the top few lines of a specified file, but will also do the same with a redirected output of another Unix/Linux command.

Show the top 10 lines with head command

By default, you just need to specify the file you’re interested in. head will get the first top 10 lines from the file and show them in your console:

[greys@rhel8 ~]$ head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

Show a specific number of lines with head command

You can specify the exact number of lines if you want. Really useful if you don’t need all 10 lines, for example:

[greys@rhel8 ~]$ head -3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

See Also