10+ useful tail command’s switches with example – Unix/Linux
The tail command
By default, the tail command prints the last 10 lines from the file which is given followed by the tail command.
In the case of more than one file with tail command displays separate output with a header identifying the file name. Tail command has a lot of useful and also very helpful switches.
In this article, we discuss about the usages of Linux tail command.
Useful switches with example:
“I’ve copied the ‘/etc/passwd’ and ‘/etc/shadow’ files to pass.txt and sha.txt respectively to demonstrate examples.”
1. tail : The tail command alone
Which displays the last 10 line by-default.
Example:
# tail pass.txt
cpanelphppgadmin:x:32004:32004::/var/cpanel/userhomes/cpanelphppgadmin:/usr/local/cpanel/bin/noshell
......................
......................
tomcat:x:495:99:Apache Tomcat:/home/tomcat:/bin/nologin
crybit:x:503:504::/home/crybit:/usr/local/cpanel/bin/noshell
2. tail : More than one file
Displays separate output with a header identifying the file name.
Example:
# tail pass.txt sha.txt ==> pass.txt <== cpanelphppgadmin:x:32004:32004::/var/cpanel/userhomes/cpanelphppgadmin:/usr/local/cpanel/bin/noshell ...................... ...................... tomcat:x:495:99:Apache Tomcat:/home/tomcat:/bin/nologin crybit:x:503:504::/home/crybit:/usr/local/cpanel/bin/noshell ==> sha.txt <== cpanelphppgadmin:*:16073:::::: ...................... ...................... tomcat:!!:16074:::::: crybit:$6$xbycY1hCeTldon5.$.J6ZtPA77351rLQdo23tKi4o5cTrjlJOOzkZJs11AeMqeGlsnTdLzuo2x7HtOzf0JigQ42AlrfTRzj5Bdcl/D.:16085:0:99999:7:::
3. tail -n : Displays last “n” lines from the text file
Example:
# tail -n3 pass.txt
cpses:x:496:497::/var/cpanel/cpses:/sbin/nologin
tomcat:x:495:99:Apache Tomcat:/home/tomcat:/bin/nologin
crybit:x:503:504::/home/crybit:/usr/local/cpanel/bin/noshell
4. -c, –bytes=K
Output the last K bytes; alternatively, use -c +K to output bytes starting with the Kth of each file.
Example:
# tail -c 100 pass.txt
pache Tomcat:/home/tomcat:/bin/nologin
crybit:x:503:504::/home/crybit:/usr/local/cpanel/bin/noshell
5. -f, –follow[={name|descriptor}]
Output appended data as the file grows; -f, –follow, and –follow=descriptor are equivalent
6. -F same as –follow=name –retry
7. –max-unchanged-stats=N
with –follow=name, reopen a FILE which has not changed size after N (default 5) iterations to see if it has been unlinked or renamed (this is the usual case of rotated log files). With inotify, this option is rarely useful.
8. –pid=PID
With -f, terminate after process ID, PID dies
9. -q, –quiet, –silent
Never output headers giving file names
10. –retry
Keep trying to open a file even when it is or becomes inaccessible; useful when following by name, i.e., with –follow=name
11. -s, –sleep-interval=N
With -f, sleep for approximately N seconds (default 1.0) between iterations.
12. –version
Displays the version.
13. –help
Displays the help messages.
The “-f” switch is very useful for analyzing the log files dynamically.
Examples: Watching apache error log dynamically.
# tail -f /usr/local/apache/logs/error_log
Or
# tailf /usr/local/apache/logs/error_log
That’s it. 🙂
Related Links:
The head command
iftop command
find command switches with example
Top command
PS command usages with example
More:
groupdel, groupmems, groupmod, useradd , usermod , chgrp, chown, ls
4 thoughts on “Tail command switches with example in Unix/Linux”