tail command

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]…

Note that all mandatory arguments used by the long option command is also required by the short option.

Options:

OptionLong nameDescription
-c–bytes=NThis 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.
-FThis is the same as using –follow=name –retry together.
-n–lines=NThis option displays N lines of the file starting at the end of the file.
–max-unchanged-stats=NWith –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=PIDThis option is used aling with -f. It terminates after process ID, PID dies.
-q–quiet –silentNever output headers giving file names.
–retryKeeps 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=SThis option is used with -f. It sleeps for approximately S seconds (default 1.0) between iterations.
-v–verboseAlways output headers giving file names.

Examples

tail – This example is the base command used on a file named text.txt.

 # tail test.txt Duis eget enim et odio consequat egestas in et massa. Curabitur vel justo posuere, consectetur nisl vel, ultrices nunc. In eget lacinia mi.  Etiam libero purus, euismod eu est a, aliquet vestibulum risus. Proin tempor, lacus eu porttitor ornare, nunc massa posuere nisl, sit amet malesuada elit lacus quis justo. Morbi auctor eu odio quis porta. Morbi lacus lectus, volutpat non pellentesque eu, gravida sed tortor. Donec commodo imperdiet porttitor. Vestibulum volutpat augue et odio rutrum, at fermentum ante aliquam. Quisque eleifend adipiscing est nec tincidunt.

tail -c 300 – In this example, we are listing the last 300 bytes of the test.txt file.

 # tail -c 300 test.txt sit amet malesuada elit lacus quis justo. Morbi auctor eu odio quis porta. Morbi lacus lectus, volutpat non pellentesque eu, gravida sed tortor. Donec commodo imperdiet porttitor. Vestibulum volutpat augue et odio rutrum, at fermentum ante aliquam.

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 -n 3 test.txt test2.txt ==> test.txt <== Donec commodo imperdiet porttitor. Vestibulum volutpat augue et odio rutrum, at fermentum ante aliquam. Quisque eleifend adipiscing est nec tincidunt. ==> test2.txt <== Donec commodo imperdiet porttitor. Vestibulum volutpat augue et odio rutrum, at fermentum ante aliquam. Quisque eleifend adipiscing est nec tincidunt.

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.

 # tail -qn 3 test.txt test2.txt Donec commodo imperdiet porttitor. Vestibulum volutpat augue et odio rutrum, at fermentum ante aliquam. Quisque eleifend adipiscing est nec tincidunt. Donec commodo imperdiet porttitor. Vestibulum volutpat augue et odio rutrum, at fermentum ante aliquam. Quisque eleifend adipiscing est nec tincidunt.r

Was this article helpful? Join the conversation!