LinuxCommandLibrary

tail

TLDR

Show last 10 lines of a file

$ tail [file]
copy
Show last N lines
$ tail -n [20] [file]
copy
Show all lines starting from line N
$ tail -n +[10] [file]
copy
Follow a file (watch for new content)
$ tail -f [file]
copy
Follow multiple files
$ tail -f [file1] [file2]
copy
Follow and retry if file is recreated
$ tail -F [file]
copy
Show last N bytes
$ tail -c [100] [file]
copy

SYNOPSIS

tail [options] [file...]

DESCRIPTION

tail outputs the last part of files. By default, it shows the last 10 lines. It's commonly used to view log files and monitor file changes in real-time.
The -f (follow) option is particularly useful for monitoring log files. Tail continues reading as new lines are appended, displaying them immediately.
Using -n +N outputs starting from line N rather than the last N lines. This is useful for skipping headers or combining with head for extracting specific ranges.
Multiple files can be specified; tail shows headers indicating which file output comes from.

PARAMETERS

-n N, --lines=N

Output last N lines (or +N for starting from line N)
-c N, --bytes=N
Output last N bytes (or +N for starting from byte N)
-f, --follow
Output appended data as file grows
-F
Same as --follow=name --retry
--retry
Keep trying to open file if inaccessible
-s N, --sleep-interval=N
Sleep N seconds between iterations with -f
--pid=PID
With -f, terminate after process PID dies
-q, --quiet
Never output headers with file names
-v, --verbose
Always output headers with file names

CAVEATS

-f follows the file descriptor. If a file is deleted and recreated (common with log rotation), use -F which follows by name and retries.
For large files, tail is efficient—it seeks to near the end rather than reading the entire file.
The + syntax for -n and -c counts from the beginning (1-indexed for lines, 0-indexed for bytes). tail -n +1 outputs the entire file.

SEE ALSO

head(1), less(1), cat(1), multitail(1)

Copied to clipboard