Linux Commands – tail

Linux Commands – tail

Linux Commands – tail

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed head command in Linux which is used to print some lines from the beginning of a file or pipe.

https://cloudaffaire.com/linux-commands-head/

In this blog post, we will discuss tail command in Linux. tail command is used to print some lines from the bottom of the file and output to standard output. By default, tail command prints the last 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name. You can also use the tail command with a pipe. tail and head commands can be used together to print from any starting point and any ending point.

Linux Commands – tail:

You can use tail command to print some lines from the bottom of a file or pipe. By default, tail prints 10 lines from the bottom of a file or pipe.

You can use tail -n K or –lines=K options to output the last K lines. However, if K starts with a ‘+’, tail command starts printing with the Kth line from the start of each file, instead of from the end.

You can use tail -c K or –bytes=K options to output the last K bytes, instead of final lines. However, if K starts with a ‘+’, tail starts printing with the Kth byte from the start of each file, instead of from the end. K maybe an integer optionally followed by, one of the following multiplicative suffixes:

  • ‘b’ => 512 (“blocks”)
  • ‘KB’ => 1000 (KiloBytes)
  • ‘K’ => 1024 (KibiBytes)
  • ‘MB’ => 1000*1000 (MegaBytes)
  • ‘M’ => 1024*1024 (MebiBytes)
  • ‘GB’ => 1000*1000*1000 (GigaBytes)
  • ‘G’ => 1024*1024*1024 (GibiBytes)

You can use tail -v or –verbose options to always print the filename header. By default, tail prints the filename header only if multiple files were provided as input.

You can use tail -q or –quiet or –silent options to suppress printing of filename header. By default, the tail does not print the filename header if a single file is provided as input.

You can use tail -f or –follow[={name|descriptor}] options to loop forever trying to read more characters at the end of the file, presumably because the file is growing. If more than one file is given, ‘tail’ prints a header whenever it gets output from a different file, to indicate which file that output is from.

There are two ways to specify how you’d like to track files with this option, but that difference is noticeable only when a followed file is removed or renamed. If you’d like to continue to track the end of a growing file even after it has been unlinked, use ‘–follow=descriptor’. This is the default behavior, but it is not useful if you’re tracking a log file that may be rotated (removed or renamed, then reopened). In that case, use ‘–follow=name’ to track the named file, perhaps by reopening it periodically to see if it has been removed and recreated by some other program.

You can use tail –follow[={name|descriptor}] with –retry option to indefinitely try to open the specified file. This option is useful mainly when following (and otherwise issues a warning). tail –follow=name –retry option infinitely retries to re-open the given files until killed. tail –follow=descriptor –retry option only affects the initial open of the file, as after a successful open, ‘tail’ will start following the file descriptor. Without this option, when ‘tail’ encounters a file that doesn’t exist or is otherwise inaccessible, it reports that fact and never checks it again. the tail command also supports -F option which is equivalent to –follow=name –retry command.

You can use tail –pid=PID option to specify the process ID, PID, of the sole writer of all FILE arguments when following by name or by descriptor. This option causes the tail to automatically terminate when the process terminates. This will work properly only if the writer process and the tailing process are running on the same machine.

You can use tail -s or –sleep-interval=NUMBER options with -f option to change the number of seconds to wait between iterations (the default is 1.0). During one iteration, every specified file is checked to see if it has changed size. However, if you also specify –pid=P option, tail command checks whether process P is alive at least every NUMBER seconds.

You can use a combination of head and tail commands to output any number of lines starting and ending from anywhere.

Hope you have enjoyed this article. In the next blog post, we will discuss touch command in Linux.

 

Leave a Reply

Close Menu