head - Unix, Linux Command


Advertisements


NAME

head - output the first part of files

SYNOPSIS

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

DESCRIPTION

head by default, prints the first 10 lines of each FILE to standard output. With more than one FILE, it precedes each set of output with a header identifying the file name. If no FILE is specified, or when FILE is specified as a dash ("-"), head reads from standard input.

Options

Tag Description
-c, --bytes=[-]num print the first num bytes of each file; with a 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 identifying file names.
-v, --verbose always print headers identifying file names.
--help display a help message and exit.
--version output version information and exit.

EXAMPLES

Example-1:

To display the first ten lines of syslog:

$ head syslog

output:

root@ubuntu:/var/log# head syslog
Dec 28 05:29:02 ubuntu rsyslogd: [origin software="rsyslogd" swVersion="7.4.4" x-pid="547" x-info="http://www.rsyslog.com"] start
Dec 28 05:29:02 ubuntu rsyslogd: rsyslogd's groupid changed to 104
Dec 28 05:29:02 ubuntu rsyslogd: rsyslogd's userid changed to 101
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpuset
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpu
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpuacct
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Linux version 3.19.0-25-generic (buildd@lgw01-20) (gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) ) #26~14.04.1-Ubuntu SMP Fri Jul 24 21:16:20 UTC 2015 (Ubuntu 3.19.0-25.26~14.04.1-generic 3.19.8-ckt2)
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.19.0-25-generic root=/dev/mapper/ubuntu--vg-root ro
Dec 28 05:29:02 ubuntu kernel: [    0.000000] KERNEL supported cpus:
Dec 28 05:29:02 ubuntu kernel: [    0.000000]   Intel GenuineIntel

Example-2:

To display the first fifteen lines of myfile.txt.

$ head -15 syslog

output:

root@ubuntu:/var/log# head -15 syslog
Dec 28 05:29:02 ubuntu rsyslogd: [origin software="rsyslogd" swVersion="7.4.4" x-pid="547" x-info="http://www.rsyslog.com"] start
Dec 28 05:29:02 ubuntu rsyslogd: rsyslogd's groupid changed to 104
Dec 28 05:29:02 ubuntu rsyslogd: rsyslogd's userid changed to 101
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpuset
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpu
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpuacct
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Linux version 3.19.0-25-generic (buildd@lgw01-20) (gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) ) #26~14.04.1-Ubuntu SMP Fri Jul 24 21:16:20 UTC 2015 (Ubuntu 3.19.0-25.26~14.04.1-generic 3.19.8-ckt2)
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.19.0-25-generic root=/dev/mapper/ubuntu--vg-root ro
Dec 28 05:29:02 ubuntu kernel: [    0.000000] KERNEL supported cpus:
Dec 28 05:29:02 ubuntu kernel: [    0.000000]   Intel GenuineIntel
Dec 28 05:29:02 ubuntu kernel: [    0.000000]   AMD AuthenticAMD
Dec 28 05:29:02 ubuntu kernel: [    0.000000]   Centaur CentaurHauls
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Disabled fast string operations
Dec 28 05:29:02 ubuntu kernel: [    0.000000] e820: BIOS-provided physical RAM map:
Dec 28 05:29:02 ubuntu kernel: [    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009e7ff] usable

Example-3:

To display the first ten lines of both syslog and kern.log, with a header before each that indicates the filename.

$ head syslog kern.log

output:

root@ubuntu:/var/log# head syslog kern.log
==> syslog <==
Dec 28 05:29:02 ubuntu rsyslogd: [origin software="rsyslogd" swVersion="7.4.4" x-pid="547" x-info="http://www.rsyslog.com"] start
Dec 28 05:29:02 ubuntu rsyslogd: rsyslogd's groupid changed to 104
Dec 28 05:29:02 ubuntu rsyslogd: rsyslogd's userid changed to 101
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpuset
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpu
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpuacct
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Linux version 3.19.0-25-generic (buildd@lgw01-20) (gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) ) #26~14.04.1-Ubuntu SMP Fri Jul 24 21:16:20 UTC 2015 (Ubuntu 3.19.0-25.26~14.04.1-generic 3.19.8-ckt2)
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.19.0-25-generic root=/dev/mapper/ubuntu--vg-root ro
Dec 28 05:29:02 ubuntu kernel: [    0.000000] KERNEL supported cpus:
Dec 28 05:29:02 ubuntu kernel: [    0.000000]   Intel GenuineIntel

==> kern.log <==
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpuset
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpu
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpuacct
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Linux version 3.19.0-25-generic (buildd@lgw01-20) (gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) ) #26~14.04.1-Ubuntu SMP Fri Jul 24 21:16:20 UTC 2015 (Ubuntu 3.19.0-25.26~14.04.1-generic 3.19.8-ckt2)
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.19.0-25-generic root=/dev/mapper/ubuntu--vg-root ro
Dec 28 05:29:02 ubuntu kernel: [    0.000000] KERNEL supported cpus:
Dec 28 05:29:02 ubuntu kernel: [    0.000000]   Intel GenuineIntel
Dec 28 05:29:02 ubuntu kernel: [    0.000000]   AMD AuthenticAMD
Dec 28 05:29:02 ubuntu kernel: [    0.000000]   Centaur CentaurHauls
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Disabled fast string operations

Example-4:

To donot print headers indicating file names, use -q option.

# head -q syslog kern.log

output:

root@ubuntu:/var/log# head -q syslog kern.log
Dec 28 05:29:02 ubuntu rsyslogd: [origin software="rsyslogd" swVersion="7.4.4" x-pid="547" x-info="http://www.rsyslog.com"] start
Dec 28 05:29:02 ubuntu rsyslogd: rsyslogd's groupid changed to 104
Dec 28 05:29:02 ubuntu rsyslogd: rsyslogd's userid changed to 101
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpuset
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpu
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpuacct
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Linux version 3.19.0-25-generic (buildd@lgw01-20) (gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) ) #26~14.04.1-Ubuntu SMP Fri Jul 24 21:16:20 UTC 2015 (Ubuntu 3.19.0-25.26~14.04.1-generic 3.19.8-ckt2)
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.19.0-25-generic root=/dev/mapper/ubuntu--vg-root ro
Dec 28 05:29:02 ubuntu kernel: [    0.000000] KERNEL supported cpus:
Dec 28 05:29:02 ubuntu kernel: [    0.000000]   Intel GenuineIntel
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpuset
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpu
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpuacct
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Linux version 3.19.0-25-generic (buildd@lgw01-20) (gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) ) #26~14.04.1-Ubuntu SMP Fri Jul 24 21:16:20 UTC 2015 (Ubuntu 3.19.0-25.26~14.04.1-generic 3.19.8-ckt2)
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.19.0-25-generic root=/dev/mapper/ubuntu--vg-root ro
Dec 28 05:29:02 ubuntu kernel: [    0.000000] KERNEL supported cpus:
Dec 28 05:29:02 ubuntu kernel: [    0.000000]   Intel GenuineIntel
Dec 28 05:29:02 ubuntu kernel: [    0.000000]   AMD AuthenticAMD
Dec 28 05:29:02 ubuntu kernel: [    0.000000]   Centaur CentaurHauls
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Disabled fast string operations

Example-5:

To display the first 5,000 lines of file
 
$ head -n 5K  syslog
 
output:
Dec 28 05:29:02 ubuntu rsyslogd: [origin software="rsyslogd" swVersion="7.4.4" x-pid="547" x-info="http://www.rsyslog.com"] start
Dec 28 05:29:02 ubuntu rsyslogd: rsyslogd's groupid changed to 104
Dec 28 05:29:02 ubuntu rsyslogd: rsyslogd's userid changed to 101
....
....(4095 lines)
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Initializing cgroup subsys cpuacct
Dec 28 05:29:02 ubuntu kernel: [    0.000000] Linux version 3.19.0-25-generic (buildd@lgw01-20) (gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) ) #26~14.04.1-



Advertisements