tail (Output the last part of files)
The tail command prints the last part (the tail) of a file to the standard output. It is widely used by system administrators to monitor log files in real-time and troubleshoot system errors as they occur.
1. Basic Usage
tail [options] filename
- By default,
tailprints the last 10 lines of the file.
2. Key Options
| Option | Name | Description |
|---|---|---|
-f |
follow | Follow. Keeps the file open and appends new data to the terminal live. |
-n [number] |
lines | Prints the specified number of lines from the end. |
-F |
follow/retry | Like -f, but keeps tracking even if the file is renamed or rotated. |
+n |
from line n | Starts printing from the n-th line to the end of the file. |
3. Practical Examples
① Real-time log monitoring
Observe system events as they happen:
tail -f /var/log/syslog
② Viewing the last 50 lines
tail -n 50 app.log
③ Using with a Pipe to find a keyword at the end
tail -n 100 big_file.txt | grep "Critical"
4. [Tip] tail -f vs. tail -F
In a production environment, logs are often "rotated" (the old file is renamed and a new one is created). tail -f will stop when the original file descriptor is lost, but tail -F will wait and automatically start following the new file.