Etc : Bash - head and tail
head
To take a peep at long text files use head which displays the first 10 lines of a file and tail which displays the last 10 lines.
Try them to view the log files that Linux creates, because sooner or later you will come across an issue with Linux that you have to troubleshoot and these log files may help to resolve that.
You will find log files /var/log so using head to take a quick peek at the messages file which contains a lot of info over a period of time.
$ cd /var/log $ sudo head messages [sudo] password for bryan: Sep 5 13:01:38 lenovo rsyslogd: [origin software="rsyslogd" swVersion="8.1901.0" x-pid="421" x-info="https://www.rsyslog.com"] rsyslogd was HUPed Sep 5 13:01:38 lenovo NetworkManager[422]: <info> [1662379298.8293] Loaded device plugin: NMWifiFactory (/usr/lib/i386-linux-gnu/NetworkManager/1.14.6/libnm-device-plugin-wifi.so) Sep 5 13:01:39 lenovo NetworkManager[422]: <info> [1662379299.0651] Loaded device plugin: NMWwanFactory (/usr/lib/i386-linux-gnu/NetworkManager/1.14.6/libnm-device-plugin-wwan.so) Sep 5 13:01:39 lenovo NetworkManager[422]: <info> [1662379299.3308] Loaded device plugin: NMBluezManager (/usr/lib/i386-linux-gnu/NetworkManager/1.14.6/libnm-device-plugin-bluetooth.so) Sep 5 13:01:39 lenovo NetworkManager[422]: <info> [1662379299.4282] Loaded device plugin: NMTeamFactory (/usr/lib/i386-linux-gnu/NetworkManager/1.14.6/libnm-device-plugin-team.so) Sep 5 13:01:39 lenovo NetworkManager[422]: <info> [1662379299.5247] device (lo): carrier: link connected Sep 5 13:01:39 lenovo NetworkManager[422]: <info> [1662379299.5258] manager: (lo): new Generic device (/org/freedesktop/NetworkManager/Devices/1) Sep 5 13:01:39 lenovo NetworkManager[422]: <info> [1662379299.5295] manager: (ens2): new Ethernet device (/org/freedesktop/NetworkManager/Devices/2) Sep 5 13:01:39 lenovo NetworkManager[422]: <info> [1662379299.5407] keyfile: add connection /run/NetworkManager/system-connections/Wired connection 1.nmconnection (313e11b1-7402-3b9f-9b0a-115b6edc67da,"Wired connection 1") Sep 5 13:01:39 lenovo NetworkManager[422]: <info> [1662379299.5427] settings: (ens2): created default wired connection 'Wired connection 1'
You can use head to look at multiple files and state how many lines you want, for
instance to take a peek at the first five lines of all the .log files in /var/log do a head -5 *.log
or if you like typing head ╴╴lines 5 *.log
.
$ sudo head -5 *.log (I had to use sudo otherwise I would not be able to read most of the log files) ==> alternatives.log <== update-alternatives 2022-08-03 12:34:19: run with --quiet --install /run/php/php-fpm.sock php-fpm.sock /run/php/php7.4-fpm.sock 74 update-alternatives 2022-08-03 12:34:19: auto-repair link group php-fpm.sock update-alternatives 2022-08-08 17:12:28: run with --quiet --install /run/php/php-fpm.sock php-fpm.sock /run/php/php7.4-fpm.sock 74 update-alternatives 2022-08-08 17:12:28: auto-repair link group php-fpm.sock update-alternatives 2022-08-09 12:27:00: run with --quiet --install /run/php/php-fpm.sock php-fpm.sock /run/php/php7.4-fpm.sock 74 ==> auth.log <== Aug 9 07:16:10 debian systemd-logind[516]: System is powering down. Aug 9 12:26:43 debian systemd-logind[511]: New seat seat0. Aug 9 12:26:43 debian systemd-logind[511]: Watching system buttons on /dev/input/event3 (Power Button) Aug 9 12:26:43 debian systemd-logind[511]: Watching system buttons on /dev/input/event1 (Power Button) Aug 9 12:26:43 debian systemd-logind[511]: Watching system buttons on /dev/input/event2 (Lid Switch) ==> boot.log <== ------------ Tue Aug 09 12:26:39 IST 2022 ------------ Debian11: clean, 206460/6545408 files, 9794933/26176000 blocks [ OK ] Finished Tell Plymouth To Write Out Runtime Data. Mounting Arbitrary Executable File Formats File System... [ OK ] Mounted Arbitrary Executable File Formats File System. ==> daemon.log <== Aug 9 07:15:43 debian systemd[1]: man-db.service: Succeeded. Aug 9 07:15:43 debian systemd[1]: Finished Daily man-db regeneration. Aug 9 07:16:05 debian gvfsd[1128]: A connection to the bus can't be made Aug 9 07:16:07 debian systemd[926]: Stopping D-Bus User Message Bus... Aug 9 07:16:08 debian systemd[926]: xdg-desktop-portal.service: Succeeded. (and the first 5 lines of 6 more files!!)
Other options to note are ..
- If the file names are not displayed ╴v or ╴╴verbose
- If you do not want the file names displayed ╴q or ╴╴quite
- If you want to see all the file bar the last n lines ╴╴lines ╴n
note the minus sign before n, also you have to use the full named option!!
You can pipe the output of a command to head, for instance the dmesg command that displays Linux kernel messages starting from when the PC was turned on.
If you run the command as sudo without any options, the output shoots by and stops leaving you to see the end. The dmesg output is so long that you will not be able to scroll back to the start.

In order to see the first 10 lines of that command do the following, you lose the colour but at least the detail won't take up all your command window!!
$ sudo dmesg | head [sudo] password for bryan: [ 0.000000] Linux version 4.19.0-23-686-pae (debian-kernel@lists.debian.org) (gcc version 8.3.0 (Debian 8.3.0-6)) #1 SMP Debian 4.19.269-1 (2022-12-20) [ 0.000000] Disabled fast string operations [ 0.000000] x86/fpu: x87 FPU will use FXSAVE [ 0.000000] BIOS-provided physical RAM map: [ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009efff] usable [ 0.000000] BIOS-e820: [mem 0x000000000009f000-0x000000000009ffff] reserved [ 0.000000] BIOS-e820: [mem 0x00000000000d2000-0x00000000000d3fff] reserved [ 0.000000] BIOS-e820: [mem 0x00000000000dc000-0x00000000000fffff] reserved [ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x000000003f6cffff] usable [ 0.000000] BIOS-e820: [mem 0x000000003f6d0000-0x000000003f6defff] ACPI data
With tail you can view the last 10 lines of a file, you can also, as is the case with head, pipe the output from a command to it. We piped the dmesg command to head to see the first 10 lines of Linux kernel messages but it's more useful to see the last ten kernel messages by piping that command to tail.
For example before you use dd to burn an image to a USB key, plug the target USB key into your computer and then run dmesg | tail to see the last 10 events. You can see how Linux viewed it, probably /dev/sdb (unless you have more than one hard drive). This will be the of=/dev/sdx part of the dd command so preventing nuking your hard drive /dev/sda :-).
$ sudo dmesg | tail [ 6102.632803] scsi host7: usb-storage 2-1.3:1.0 [ 6103.639074] scsi 7:0:0:0: Direct-Access General UDisk 5.00 PQ: 0 ANSI: 2 [ 6103.639981] sd 7:0:0:0: Attached scsi generic sg3 type 0 [ 6103.640675] sd 7:0:0:0: [sdb] 15728640 512-byte logical blocks: (8.05 GB/7.50 GiB) [ 6103.641276] sd 7:0:0:0: [sdb] Write Protect is off [ 6103.641281] sd 7:0:0:0: [sdb] Mode Sense: 0b 00 00 08 [ 6103.642029] sd 7:0:0:0: [sdb] No Caching mode page found [ 6103.642038] sd 7:0:0:0: [sdb] Assuming drive cache: write through [ 6103.673410] sdb: sdb1 [ 6103.708545] sd 7:0:0:0: [sdb] Attached SCSI removable disk (so the USB key is referred to as /dev/sdb and its first partition is /dev/sdb1)
tail also has a very handy option -f --follow which will show the outputs of a file as it changes, to play with this do the following....
Open a terminal and go to your sandbox folder and start to type in the first command shown in the Tab 1 column of the table below. Then open up a new tab Ctrl+Shift+Tab and in there type in the first command in the Tab 2 column.
Work your way through the commands bopping between tabs - Alt+1 and Alt+2
Tab 1 | Tab 2 |
---|---|
(writing testing.txt) $ cat > testing.txt one two three ^C (stopping cat) |
(following testing.txt) $ tail -f testing.txt one two three |
(Adding to testing.txt) $ cat >> testing.txt four five six ^C (stopping cat) |
(see the changes as they happen) $ tail -f testing.txt one two three four five six |
(over writing testing.txt) $ cat > testing.txt seven eight nine ^C (The final file) $cat testing.txt seven eight nine |
(Yep the file just got shorter!!) tail: testing.txt: file truncated seven eight nine ^C (stopping tail) |
Tail has a similar option to head to skipping lines in that you can view all the file bar the first n lines using the option -n +NUM with NUM being the number of lines that you want to skip.
To see how useful this option is go to the /etc/systemd folder which contains a number of *.conf files for systemd a system and service manager for Linux operating systems developed by Red Hat and adopted by most Linux Distros.
You would be very disappointed if you used head to take a quick peek at these files as each one starts off with the same 12 lines of code, not to worry tail to the rescue using the option above to view all the .conf files without the repeating 12 lines!!
$ cd /etc/systemd $ls ls *.conf journald.conf networkd.conf sleep.conf timesyncd.conf logind.conf resolved.conf system.conf user.conf $ tail *.conf -n +12 (just three .conf files shown) ==> networkd.conf <== [DHCP] #DUIDType=vendor #DUIDRawData= ==> resolved.conf <== [Resolve] #DNS= #FallbackDNS= #Domains= #LLMNR=yesI #MulticastDNS=yes #DNSSEC=allow-downgrade #DNSOverTLS=no #Cache=yes #DNSStubListener=yes #ReadEtcHosts=yes ==> sleep.conf <== [Sleep] #AllowSuspend=yes #AllowHibernation=yes #AllowSuspendThenHibernate=yes #AllowHybridSleep=yes #SuspendMode= #SuspendState=mem standby freeze #HibernateMode=platform shutdown #HibernateState=disk #HybridSleepMode=suspend platform shutdown #HybridSleepState=disk #HibernateDelaySec=180min .....
A final note on dmesg: use the options below for a better experience!!
-H, --human human readable output -T, --ctime show human-readable timestamp (may be inaccurate!)

This causes dmesg to use the less command that allows you to scroll up and down a large text file plus a whole lot more!!.
Coming soon an article on less, but if you can't wait read it's man page now!!