Linux head and tail are basic commands for displaying file contents. In this post we’re going to show how to do that on many examples!
In the following examples we’re going to use dmesg command as our source of text data. The command prints Linux kernal information.
Head of file
Display the first 10 lines (default) of a file
dmesg | head
Prints the following:
[ 0.000000] microcode: CPU0 microcode updated early to revision 0x1c, date = 2015-02-26 [ 0.000000] Initializing cgroup subsys cpuset [ 0.000000] Initializing cgroup subsys cpu [ 0.000000] Initializing cgroup subsys cpuacct [ 0.000000] Linux version 4.4.0-38-generic (buildd@lgw01-58) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.2) ) #57-Ubuntu SMP Tue Sep 6 15:42:33 UTC 2016 (Ubuntu 4.4.0-38.57-generic 4.4.19) [ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-4.4.0-38-generic root=UUID=4640b17f-3d95-42ed-aa89-7cddd0274067 ro quiet splash vt.handoff=7 [ 0.000000] KERNEL supported cpus: [ 0.000000] Intel GenuineIntel [ 0.000000] AMD AuthenticAMD [ 0.000000] Centaur CentaurHauls
Display given number of lines from the beginning of a file
dmesg | head -5
Prints the following:
[ 0.000000] microcode: CPU0 microcode updated early to revision 0x1c, date = 2015-02-26 [ 0.000000] Initializing cgroup subsys cpuset [ 0.000000] Initializing cgroup subsys cpu [ 0.000000] Initializing cgroup subsys cpuacct [ 0.000000] Linux version 4.4.0-38-generic (buildd@lgw01-58) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.2) ) #57-Ubuntu SMP Tue Sep 6 15:42:33 UTC 2016 (Ubuntu 4.4.0-38.57-generic 4.4.19)
Display the first 30 characters of a file
dmesg | head -c 30
Note: No newline at the end.
[ 0.000000] microcode: CPU0
Tail of file
Display the last 10 lines (default) of a file
dmesg | tail
Prints the following:
[ 92.924678] cfg80211: Regulatory domain changed to country: PL [ 92.924680] cfg80211: DFS Master region: ETSI [ 92.924682] cfg80211: (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time) [ 92.924684] cfg80211: (2402000 KHz - 2482000 KHz @ 40000 KHz), (N/A, 2000 mBm), (N/A) [ 92.924687] cfg80211: (5170000 KHz - 5250000 KHz @ 80000 KHz, 160000 KHz AUTO), (N/A, 2000 mBm), (N/A) [ 92.924689] cfg80211: (5250000 KHz - 5330000 KHz @ 80000 KHz, 160000 KHz AUTO), (N/A, 2000 mBm), (0 s) [ 92.924691] cfg80211: (5490000 KHz - 5710000 KHz @ 160000 KHz), (N/A, 2700 mBm), (0 s) [ 92.924693] cfg80211: (57000000 KHz - 66000000 KHz @ 2160000 KHz), (N/A, 4000 mBm), (N/A) [ 7167.784912] perf interrupt took too long (2503 > 2500), lowering kernel.perf_event_max_sample_rate to 50000 [ 7625.125962] usb 2-1.4: reset high-speed USB device number 3 using ehci-pci
Display given number of lines from the end of a file
dmesg | tail -5 dmesg | tail -n 5 # the same
Prints the following:
[ 92.924689] cfg80211: (5250000 KHz - 5330000 KHz @ 80000 KHz, 160000 KHz AUTO), (N/A, 2000 mBm), (0 s) [ 92.924691] cfg80211: (5490000 KHz - 5710000 KHz @ 160000 KHz), (N/A, 2700 mBm), (0 s) [ 92.924693] cfg80211: (57000000 KHz - 66000000 KHz @ 2160000 KHz), (N/A, 4000 mBm), (N/A) [ 7167.784912] perf interrupt took too long (2503 > 2500), lowering kernel.perf_event_max_sample_rate to 50000 [ 7625.125962] usb 2-1.4: reset high-speed USB device number 3 using ehci-pci
Display the last 30 characters of a file
dmesg | tail -c 30
Prints the following:
evice number 3 using ehci-pci
Middle of file
Display lines between 3 and 5
dmesg | head -5 | tail -2
Prints the following:
[ 0.000000] Initializing cgroup subsys cpuacct [ 0.000000] Linux version 4.4.0-38-generic (buildd@lgw01-58) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.2) ) #57-Ubuntu SMP Tue Sep 6 15:42:33 UTC 2016 (Ubuntu 4.4.0-38.57-generic 4.4.19)
References:
- GNU Coreutils: head
- GNU Coreutils: tail
- Check out Bash Tutorial to learn more cool things about Bash!