coreutils: tail invocation
1
1 5.2 ‘tail’: Output the last part of files
1 =========================================
1
1 ‘tail’ prints the last part (10 lines by default) of each FILE; it reads
1 from standard input if no files are given or when given a FILE of ‘-’.
1 Synopsis:
1
1 tail [OPTION]... [FILE]...
1
1 If more than one FILE is specified, ‘tail’ prints a one-line header
1 before the output for each FILE, consisting of:
1
1 ==> FILE NAME <==
1
1 For further processing of tail output, it can be useful to convert
1 the file headers to line prefixes, which can be done like:
1
1 tail ... |
1 awk '
1 /^==> .* <==$/ {prefix=substr($0,5,length-8)":"; next}
1 {print prefix$0}
1 ' | ...
1
1 GNU ‘tail’ can output any amount of data (some other versions of
1 ‘tail’ cannot). It also has no ‘-r’ option (print in reverse), since
1 reversing a file is really a different job from printing the end of a
1 file; BSD ‘tail’ (which is the one with ‘-r’) can only reverse files
1 that are at most as large as its buffer, which is typically 32 KiB. A
1 more reliable and versatile way to reverse files is the GNU ‘tac’
1 command.
1
11 The program accepts the following options. Also see ⇒Common
options.
1
1 ‘-c [+]NUM’
1 ‘--bytes=[+]NUM’
1 Output the last NUM bytes, instead of final lines. However, if NUM
1 is prefixed with a ‘+’, start printing with byte NUM from the start
1 of each file, instead of from the end. NUM may be, or may be an
1 integer optionally followed by, one of the following multiplicative
1 suffixes:
1 ‘b’ => 512 ("blocks")
1 ‘KB’ => 1000 (KiloBytes)
1 ‘K’ => 1024 (KibiBytes)
1 ‘MB’ => 1000*1000 (MegaBytes)
1 ‘M’ => 1024*1024 (MebiBytes)
1 ‘GB’ => 1000*1000*1000 (GigaBytes)
1 ‘G’ => 1024*1024*1024 (GibiBytes)
1 and so on for ‘T’, ‘P’, ‘E’, ‘Z’, and ‘Y’.
1
1 ‘-f’
1 ‘--follow[=HOW]’
1 Loop forever trying to read more characters at the end of the file,
1 presumably because the file is growing. If more than one file is
1 given, ‘tail’ prints a header whenever it gets output from a
1 different file, to indicate which file that output is from.
1
1 There are two ways to specify how you’d like to track files with
1 this option, but that difference is noticeable only when a followed
1 file is removed or renamed. If you’d like to continue to track the
1 end of a growing file even after it has been unlinked, use
1 ‘--follow=descriptor’. This is the default behavior, but it is not
1 useful if you’re tracking a log file that may be rotated (removed
1 or renamed, then reopened). In that case, use ‘--follow=name’ to
1 track the named file, perhaps by reopening it periodically to see
1 if it has been removed and recreated by some other program. Note
1 that the inotify-based implementation handles this case without the
1 need for any periodic reopening.
1
1 No matter which method you use, if the tracked file is determined
1 to have shrunk, ‘tail’ prints a message saying the file has been
1 truncated and resumes tracking the end of the file from the
1 newly-determined endpoint.
1
1 When a file is removed, ‘tail’’s behavior depends on whether it is
1 following the name or the descriptor. When following by name, tail
1 can detect that a file has been removed and gives a message to that
1 effect, and if ‘--retry’ has been specified it will continue
1 checking periodically to see if the file reappears. When following
1 a descriptor, tail does not detect that the file has been unlinked
1 or renamed and issues no message; even though the file may no
1 longer be accessible via its original name, it may still be
1 growing.
1
1 The option values ‘descriptor’ and ‘name’ may be specified only
1 with the long form of the option, not with ‘-f’.
1
1 The ‘-f’ option is ignored if no FILE operand is specified and
1 standard input is a FIFO or a pipe. Likewise, the ‘-f’ option has
1 no effect for any operand specified as ‘-’, when standard input is
1 a FIFO or a pipe.
1
1 With kernel inotify support, output is triggered by file changes
1 and is generally very prompt. Otherwise, ‘tail’ sleeps for one
1 second between checks— use ‘--sleep-interval=N’ to change that
1 default—which can make the output appear slightly less responsive
1 or bursty. When using tail without inotify support, you can make
1 it more responsive by using a sub-second sleep interval, e.g., via
1 an alias like this:
1
1 alias tail='tail -s.1'
1
1 ‘-F’
1 This option is the same as ‘--follow=name --retry’. That is, tail
1 will attempt to reopen a file when it is removed. Should this
1 fail, tail will keep trying until it becomes accessible again.
1
1 ‘--max-unchanged-stats=N’
1 When tailing a file by name, if there have been N (default n=5)
1 consecutive iterations for which the file has not changed, then
1 ‘open’/‘fstat’ the file to determine if that file name is still
1 associated with the same device/inode-number pair as before. When
1 following a log file that is rotated, this is approximately the
1 number of seconds between when tail prints the last pre-rotation
1 lines and when it prints the lines that have accumulated in the new
1 log file. This option is meaningful only when polling (i.e.,
1 without inotify) and when following by name.
1
1 ‘-n [+]NUM’
1 ‘--lines=[+]’
1 Output the last NUM lines. However, if NUM is prefixed with a ‘+’,
1 start printing with line NUM from the start of each file, instead
1 of from the end. Size multiplier suffixes are the same as with the
1 ‘-c’ option.
1
1 ‘--pid=PID’
1 When following by name or by descriptor, you may specify the
1 process ID, PID, of the sole writer of all FILE arguments. Then,
1 shortly after that process terminates, tail will also terminate.
1 This will work properly only if the writer and the tailing process
1 are running on the same machine. For example, to save the output
1 of a build in a file and to watch the file grow, if you invoke
1 ‘make’ and ‘tail’ like this then the tail process will stop when
1 your build completes. Without this option, you would have had to
1 kill the ‘tail -f’ process yourself.
1
1 $ make >& makerr & tail --pid=$! -f makerr
1
1 If you specify a PID that is not in use or that does not correspond
1 to the process that is writing to the tailed files, then ‘tail’ may
1 terminate long before any FILEs stop growing or it may not
1 terminate until long after the real writer has terminated. Note
1 that ‘--pid’ cannot be supported on some systems; ‘tail’ will print
1 a warning if this is the case.
1
1 ‘-q’
1 ‘--quiet’
1 ‘--silent’
1 Never print file name headers.
1
1 ‘--retry’
1 Indefinitely try to open the specified file. This option is useful
1 mainly when following (and otherwise issues a warning).
1
1 When following by file descriptor (i.e., with
1 ‘--follow=descriptor’), this option only affects the initial open
1 of the file, as after a successful open, ‘tail’ will start
1 following the file descriptor.
1
1 When following by name (i.e., with ‘--follow=name’), ‘tail’
1 infinitely retries to re-open the given files until killed.
1
1 Without this option, when ‘tail’ encounters a file that doesn’t
1 exist or is otherwise inaccessible, it reports that fact and never
1 checks it again.
1
1 ‘-s NUMBER’
1 ‘--sleep-interval=NUMBER’
1 Change the number of seconds to wait between iterations (the
1 default is 1.0). During one iteration, every specified file is
1 checked to see if it has changed size. Historical implementations
1 of ‘tail’ have required that NUMBER be an integer. However, GNU
11 ‘tail’ accepts an arbitrary floating point number. ⇒Floating
point. When ‘tail’ uses inotify, this polling-related option is
1 usually ignored. However, if you also specify ‘--pid=P’, ‘tail’
1 checks whether process P is alive at least every NUMBER seconds.
1
1 ‘-v’
1 ‘--verbose’
1 Always print file name headers.
1
1 ‘-z’
1 ‘--zero-terminated’
1 Delimit items with a zero byte rather than a newline (ASCII LF).
1 I.e., treat input as items separated by ASCII NUL and terminate
1 output items with ASCII NUL. This option can be useful in
1 conjunction with ‘perl -0’ or ‘find -print0’ and ‘xargs -0’ which
1 do the same in order to reliably handle arbitrary file names (even
1 those containing blanks or other special characters).
1
1 For compatibility ‘tail’ also supports an obsolete usage ‘tail
1 -[NUM][bcl][f] [FILE]’, which is recognized only if it does not conflict
1 with the usage described above. This obsolete form uses exactly one
1 option and at most one file. In the option, NUM is an optional decimal
1 number optionally followed by a size letter (‘b’, ‘c’, ‘l’) to mean
1 count by 512-byte blocks, bytes, or lines, optionally followed by ‘f’
1 which has the same meaning as ‘-f’.
1
1 On systems not conforming to POSIX 1003.1-2001, the leading ‘-’ can
1 be replaced by ‘+’ in the traditional option syntax with the same
1 meaning as in counts, and on obsolete systems predating POSIX
1 1003.1-2001 traditional usage overrides normal usage when the two
1 conflict. This behavior can be controlled with the ‘_POSIX2_VERSION’
1 environment variable (⇒Standards conformance).
1
1 Scripts intended for use on standard hosts should avoid traditional
1 syntax and should use ‘-c NUM[b]’, ‘-n NUM’, and/or ‘-f’ instead. If
1 your script must also run on hosts that support only the traditional
1 syntax, you can often rewrite it to avoid problematic usages, e.g., by
1 using ‘sed -n '$p'’ rather than ‘tail -1’. If that’s not possible, the
1 script can use a test like ‘if tail -c +1 </dev/null >/dev/null 2>&1;
1 then ...’ to decide which syntax to use.
1
1 Even if your script assumes the standard behavior, you should still
1 beware usages whose behaviors differ depending on the POSIX version.
1 For example, avoid ‘tail - main.c’, since it might be interpreted as
1 either ‘tail main.c’ or as ‘tail -- - main.c’; avoid ‘tail -c 4’, since
1 it might mean either ‘tail -c4’ or ‘tail -c 10 4’; and avoid ‘tail +4’,
1 since it might mean either ‘tail ./+4’ or ‘tail -n +4’.
1
1 An exit status of zero indicates success, and a nonzero value
1 indicates failure.
1