Introduction
The tail command is used to output the last part of files. If you are examining a huge file of logs or any file you are examining and you are only interested with the last few lines of the text file, then tail becomes your good buddy.
Syntax
The command comes with many options as you may guess but the basic syntax is as follows:
$ tail [OPTION]… [FILE]…
The various options for the tail are as follows:
- –c, –bytes=[+]NUM output the last NUM bytes; or use -c +NUM to output starting with byte NUM of each file
- -f, –follow[={name|descriptor}] output appended data as the file grows; an absent option argument means ‘descriptor’
- -F same as –follow=name –retry
- –n, –lines=[+]NUM output the last NUM lines, instead of the last 10; or use -n +NUM to output starting with line NUM
- –pid=PID with -f, terminate after process ID, PID dies
- -q, –quiet, –silent never output headers giving file names
- –retry keep trying to open a file if it is inaccessible
- -s, –sleep-interval=N with -f, sleep for approximately N seconds (default 1.0) between iterations; with inotify and –pid=P, check process P at least once every N seconds
- -v, –verbose always output headers giving file names
- -z, –zero-terminated line delimiter is NUL, not newline
Examples of the tail at work
To view the last 20 bytes worth of data in the /var/log/secure file
$ tail -c 20 /var/log/secure
To view the last lines of /var/log/secure file as it gets updated in real-time.
$ tail -f /var/log/secure
You can also view multiple files in the same way as shown below:
$ tail -f /var/log/secure /var/log/messages
To view the last 100 lines in the /var/log/secure file
$ tail -n 100 /var/log/secure
To view the last lines in the /var/log/alternatives.log file with headers outputted.
$ tail -vf /var/log/alternatives.log ==> /var/log/alternatives.log <== update-alternatives 2019-07-17 15:37:30: link group fakeroot updated to point to /usr/bin/fakeroot-sysv
If you do not want nay headers, use -q option for quiet
$ tail -vf /var/log/alternatives.log update-alternatives 2019-07-17 15:37:30: link group fakeroot updated to point to /usr/bin/fakeroot-sysv
Conclusion
It is sometimes necessary to keep a track of the data being written on a log file in real time to check for the problems that are being experienced by your application or process. The tail can be a real time saver and a convenient tool to keep you updated in the scenario. Thank you for reading through and be sure to check out other guides similar to this one below:
How to use scp command to securely transfer files with examples
Top 10 Free Backup software for Linux
netstat vs ss usage guide on Linux
How to use rsync command on Linux/Unix with examples
How to Check TCP connections States in Linux with Netstat
How to extract .xz files on Linux – CenOS / Ubuntu / Debian