tail
Display the end of a file
TLDR
Show last 'count' lines in file
Print a file from a specific line number
Print a specific count of bytes from the end of a given file
Print the last lines of a given file and keep reading it until
Keep reading file until
Show last 'num' lines in 'file' and refresh every 'n' seconds
SYNOPSIS
tail [OPTION]... [FILE]...
PARAMETERS
-n NUM, --lines=NUM
Output the last NUM lines. NUM can be prefixed with '+' to start outputting from the NUMth line of each file, rather than displaying the last NUM lines. A negative NUM is equivalent to a plain NUM.
-c NUM, --bytes=NUM
Output the last NUM bytes. Similar to -n, NUM can be prefixed with '+' to start outputting from the NUMth byte.
-f, --follow[={name|descriptor}]
Output appended data as the file grows. If the file is renamed or rotated (e.g., by logrotate), --follow=descriptor (default) continues to track the original file descriptor, while --follow=name will reopen the file by name, thus tracking the new file after rotation.
-F
Effectively the same as --follow=name --retry. This is a common and convenient option for robust log monitoring, as it continuously tries to open a file by name, even if it's currently unavailable or rotated.
-q, --quiet, --silent
Never output headers giving file names when processing multiple files.
-s NUM, --sleep-interval=NUM
With -f, sleep for approximately NUM seconds (default 1.0) between checks for new data. This reduces CPU usage when monitoring files that are updated infrequently.
-v, --verbose
Always output headers giving file names, even when only processing a single file.
-z, --decompress
Decompress gzipped files before displaying their content.
--retry
With -f, keep trying to open a file even if it is initially inaccessible or disappears. Useful for monitoring files that might not exist yet or are subject to rotation.
--pid=PID
With -f, terminate tail after process ID PID dies. Useful for monitoring logs related to a specific running process.
--max-unchanged-stats=N
With -f, terminate after N iterations of a file having unchanged size/inode. This prevents tail from running indefinitely on a file that has stopped being updated.
DESCRIPTION
The tail command is a fundamental Unix and Linux utility used to output the last part of files. By default, it prints the last 10 lines of each specified file to standard output. It is most commonly used for monitoring log files in real-time, as its --follow option allows it to continuously display new lines appended to a file as they are written. This makes tail indispensable for system administrators and developers for debugging, tracking system events, and observing application logs without needing to repeatedly open and close the file. It can display content by lines or by bytes and offers various options to control its output and behavior, such as displaying headers for multiple files, quiet mode, and handling file rotation.
CAVEATS
When tail is used with multiple FILE arguments, it prefixes the output for each file with a header indicating the filename, unless -q is used.
The --follow option is powerful for log monitoring but needs careful consideration for how it interacts with log rotation schemes. --follow=descriptor (the default -f) will continue reading the old file after it's been rotated, which might not be desired. --follow=name or -F is generally preferred for robust log monitoring as it tracks the file by its name and automatically switches to the new log file created after rotation, often in combination with --retry.
Using --bytes or --lines with large numbers on very large files can be slow as tail might need to seek backward through a significant portion of the file.
DEFAULT BEHAVIOR
If no FILE is specified, or when FILE is -, tail reads from standard input. By default, without any options like -n or -c, tail outputs the last 10 lines.
EXITING TAIL -F
To stop a running tail -f command, press Ctrl+C.
NUMBERING WITH +NUM
When NUM is prefixed with + (e.g., tail -n +100 file.log), tail starts outputting from the specified line number (or byte number with -c) until the end of the file, rather than showing the last NUM lines/bytes. This effectively shows the file from a certain point.
HISTORY
The tail command is a standard utility in Unix-like operating systems, part of the coreutils package on Linux. Its functionality has been a staple since the early days of Unix, providing a simple yet powerful way to inspect the end of text files. The --follow option, a widely used feature, was a significant enhancement, transforming tail into a real-time log monitoring tool, distinguishing it from simpler utilities that only display static content. Its design is aligned with the Unix philosophy of providing small, single-purpose tools that can be combined to perform complex tasks.