Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Linux head tail – display lines of file by example

farenda 2016-09-30 0

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!
Share with the World!
Categories Bash, Linux Tags bash
Previous: Linux cut command by example
Next: Bubble Sort in Java

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored