An example of head command in Linux system

Time:2021-6-9

headcommandPrint the first 10 lines of each file to standard output. For multiple files, add a header giving the file name before each file. If there is no file, or if the file is-To read the standard input.

How to use headcommand

useheaddisplay/etc/passwdThe first ten lines are as follows:

[[email protected] ~]# head /etc/passwd
root0:0:root:/root:/bin/bash
bin1:1:bin:/bin:/sbin/nologin
daemon2:2:daemon:/sbin:/sbin/nologin
adm3:4:adm:/var/adm:/sbin/nologin
lp4:7:lp:/var/spool/lpd:/sbin/nologin
sync5:0:sync:/sbin:/bin/sync
shutdown6:0:shutdown:/sbin:/sbin/shutdown
halt7:0:halt:/sbin:/sbin/halt
mail8:12:mail:/var/spool/mail:/sbin/nologin
operator11:0:operator:/root:/sbin/nologin

An example of head command in Linux system

How to display the specified number of rows

use-nCommand to display the specified number of lines, or omit the lettersn, use only-And numbers (numbers and numbers)-There are no spaces between them).

For example: Using-nParameter display/etc/passwdThe first five lines are as follows:

[[email protected] ~]# head -n 5 /etc/passwd
root0:0:root:/root:/bin/bash
bin1:1:bin:/bin:/sbin/nologin
daemon2:2:daemon:/sbin:/sbin/nologin
adm3:4:adm:/var/adm:/sbin/nologin
lp4:7:lp:/var/spool/lpd:/sbin/nologin

An example of head command in Linux system

View multiple files

If multiple files are provided as input to the head command, it will display the first ten lines of each file. The following example usesheadCommand display/etc/passwdand/etc/shadowThe first two lines of the file are as follows:

[[email protected] ~]# head -n 2 /etc/passwd /etc/shadow
==> /etc/passwd <==
root0:0:root:/root:/bin/bash
bin1:1:bin:/bin:/sbin/nologin

==> /etc/shadow <==
root:$6$a73/Rt1OEkUi04km$GX4FzCh51bqNZpygxDJIPwt2OuxwYF/Qm8UjnIYSvxzrOcIn4UR/rv4yaArJ4jl6YS//ZT8y70eeivYatF89a0::0:99999:7:::
bin:*:17834:0:99999:7:::

An example of head command in Linux system

summary

The head command prints the first 10 lines of each file to standard output. For multiple files, add a header giving the file name before each file.