Linux Commands : head and tail

head and tail are the basic commands. The reason behind writing them here is beginners can learn about this commands. head and tail both are different commands and used for different purpose.   

head  

Prints first 10 line of the file to standard output. 

Options used with this command 

-n or --lines  
It is used to print specific number of lines from file. 

For example, sudo head -n 15 /var/log/messages or sudo head --lines 15 /var/log/messages 

It will print first 15 lines of /var/log/messages. As it needs root privileges to read /var/log/messages file so I wrote sudo in the beginning. 

Output will look like this. [15 Lines] 

Jun 19 03:18:00 desktop avahi-daemon[833]: server.c: Packet too short or invalid while reading response record. (Maybe a UTF-8 problem?) 
Jun 19 03:19:33 desktop avahi-daemon[833]: server.c: Packet too short or invalid while reading response record. (Maybe a UTF-8 problem?) 
Jun 19 03:20:01 desktop systemd: Started Session 468 of user root. 
Jun 19 03:20:01 desktop systemd: Starting Session 468 of user root. 
Jun 19 03:23:35 desktop systemd: Starting dnf makecache... 
Jun 19 03:23:35 desktop dnf: cachedir: /var/cache/dnf/x86_64/7/x86_64/7 
Jun 19 03:23:35 desktop dnf: Loaded plugins: builddep, config-manager, debuginfo-install, download, generate_completion_cache, kickstart, needs-restarting, noroot, protected_packages, Query, reposync 
Jun 19 03:23:35 desktop dnf: DNF version: 0.6.4 
Jun 19 03:23:35 desktop dnf: Making cache files for all metadata files. 
Jun 19 03:23:35 desktop dnf: Metadata cache refreshed recently. 
Jun 19 03:23:35 desktop systemd: Started dnf makecache. 
Jun 19 03:28:21 desktop google-chrome.desktop: [4089:4193:0619/032821.726917:ERROR:service_manager.cc(158)] Connection InterfaceProviderSpec prevented service: content_plugin from binding interface: memory_instrumentation::mojom::Coordinator exposed by: content_browser 
Jun 19 03:28:21 desktop google-chrome.desktop: [WARNING:flash/platform/pepper/pep_module.cpp(63)] SANDBOXED 
Jun 19 03:30:01 desktop systemd: Started Session 469 of user root. 
Jun 19 03:30:01 desktop systemd: Starting Session 469 of user root. 
  
-v or --verbose 
It will always print headers by giving file name.  

For example, sudo head -v /var/log/messages or sudo head --verbose /var/log/messages 

Output will look like this. [First 10 lines with file name in the beginning.] 

==> /var/log/messages <== 
Jun 19 03:18:00 desktop avahi-daemon[833]: server.c: Packet too short or invalid while reading response record. (Maybe a UTF-8 problem?) 
Jun 19 03:19:33 desktop avahi-daemon[833]: server.c: Packet too short or invalid while reading response record. (Maybe a UTF-8 problem?) 
Jun 19 03:20:01 desktop systemd: Started Session 468 of user root. 
Jun 19 03:20:01 desktop systemd: Starting Session 468 of user root. 
Jun 19 03:23:35 desktop systemd: Starting dnf makecache... 
Jun 19 03:23:35 desktop dnf: cachedir: /var/cache/dnf/x86_64/7/x86_64/7 
Jun 19 03:23:35 desktop dnf: Loaded plugins: builddep, config-manager, debuginfo-install, download, generate_completion_cache, kickstart, needs-restarting, noroot, protected_packages, Query, reposync 
Jun 19 03:23:35 desktop dnf: DNF version: 0.6.4 
Jun 19 03:23:35 desktop dnf: Making cache files for all metadata files. 
Jun 19 03:23:35 desktop dnf: Metadata cache refreshed recently. 

tail  

It will print last 10 lines of the file.  

Options used with this command 

-n or --lines 
It will be used to print specific number of last lines.  

For example, sudo tail -n 5 /var/log/messages or sudo tail --lines 5 /var/log/messages 

Output will look like this. [5 lines] 

Jun 20 18:52:51 desktop dbus-daemon: dbus[839]: [system] Successfully activated service 'net.reactivated.Fprint' 
Jun 20 18:52:51 desktop systemd: Started Fingerprint Authentication Daemon. 
Jun 20 18:52:51 desktop fprintd: Launching FprintObject 
Jun 20 18:52:51 desktop fprintd: ** Message: D-Bus service launched with name: net.reactivated.Fprint 
Jun 20 18:52:51 desktop fprintd: ** Message: entering main loop 

-f or --follow 
It will append output as file's data grows. [Mostly used while monitoring logs.] 

For example, sudo tail -f /var/log/messages or sudo tail --follow /var/log/messages 

It will keep showing continues output so it will be hard to show you output example here but you may try it on your linux machine.  

-v or --verbose 
It will show output with file name in the beginning. [As same as it was working in head command.] 

For example, sudo tail -v /var/log/messages or sudo tail --verbose /var/log/messages 

Output will look like same as we have seen in head command but it will print last 10 lines with the file name. This is the main difference.  


Comments

Popular Posts