Table of Contents
Much like the head command, the tail command comes in handy when working with larger files. Instead of pulling the first 10 lines of a file, tail pulls the last 10 lines of a file when using the base command. It also takes numeric options to allow you to tailor the number of lines displayed.
Command: tail
Synopsis: tail [OPTION]… [FILE]…
Options:
Option | Long name | Description |
---|---|---|
-c | –bytes=N | This option displays the last N bytes of the file. |
-f | –follow[={name|descriptor}] | Output appended data as the file grows; -f, –follow, and –follow=descriptor are equivalent. This means when running the command you will get the initial 10 lines and then the additional lines will appear as they are added to the file. This is particularly useful when following an active log file. |
-F | This is the same as using –follow=name –retry together. | |
-n | –lines=N | This option displays N lines of the file starting at the end of the file. |
–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) | |
–pid=PID | This option is used aling with -f. It terminates after process ID, PID dies. | |
-q | –quiet –silent | Never output headers giving file names. |
–retry | Keeps trying to open a file even if it is inaccessible when tail starts or if it becomes inaccessible later; useful when following by name, i.e., with –follow=name. | |
-s | –sleep-interval=S | This option is used with -f. It sleeps for approximately S seconds (default 1.0) between iterations. |
-v | –verbose | Always output headers giving file names. |
Examples
tail – This example is the base command used on a file named text.txt.
tail -c 300 – In this example, we are listing the last 300 bytes of the test.txt file.
tail -n – Using the -n option allows you to specify how many lines you want to display. This example looks to display the last 3 lines from two different files (test.txt and test2.txt). Note that in this example a header is added before the outpur for each file.
tail -q – The ‘quiet’, or -q option removes the header information that is displayed before the output of a file. Using the previous example where we printed out the last 3 lines of two files, we alter the command slightly by adding the ‘quiet’ option.