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.

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

Arunlal Ashok

Linux Server Administrator. I'm dealing with Linux servers since 2012. I started this blog to share and discuss my ideas with the world. Check My Profile!! in uPwork (oDesk) and let me know if you need any assistance. Thanks!!

You may also like...

3 Responses

  1. January 24, 2014

    […] topics: The tail command iftop command find command switches with example Top command PS command usages with […]

  2. February 5, 2014

    […] commands, groupdel, groupmems, groupmod, useradd , usermod , chgrp, chown, ls, head, tail, top, ps, find, […]

  3. February 7, 2014

    […] useful commands: groupdel, groupmems, groupmod, useradd , usermod , chgrp, chown, ls, head, tail, top, ps, find, crontab ftp commands, […]

Leave a Reply

Your email address will not be published. Required fields are marked *