The tail
command is used to display the last few lines of a file. By default, it shows the last 10 lines, but you can customize the number of lines to display. This command is useful for viewing the most recent entries in log files or quickly checking the end of a file.
Initial Example
Using tail
to display the last 10 lines of a file called example.txt
:
tail example.txt
TAIL Parameters
Parameter | Description |
---|---|
-c, --bytes=[+]NUM | Display the last NUM bytes; use -c +NUM to begin from byte NUM of each file. |
-f, --follow[={name|descriptor}] | Continuously show appended data as the file grows; defaults to descriptor when no option is specified. |
-F | Equivalent to --follow=name --retry ; useful for keeping track of files that may be temporarily inaccessible. |
-n, --lines=[+]NUM | Display the last NUM lines instead of the default 10; use -n +NUM to skip the first NUM-1 lines. |
--max-unchanged-stats=N | With --follow=name , reopen a file if it hasn’t changed size after N (default 5) iterations; this is useful for rotated log files. |
--pid=PID | With -f , stop showing data after the process with ID PID terminates; can monitor multiple PIDs. |
-q, --quiet, --silent | Suppress file name headers in the output. |
--retry | Keep attempting to open a file if it is initially inaccessible. |
-s, --sleep-interval=N | With -f , pause for approximately N seconds (default 1.0) between iterations; ensures process checks every N seconds with --pid . |
-v, --verbose | Always show file name headers in the output. |
-z, --zero-terminated | Use NUL character as the line delimiter instead of newline. |
--help | Display help information and exit. |
--version | Show version information and exit. |
Examples
1. Display the Last 10 Lines of a File
To view the last 10 lines of a file (default behavior):
tail filename.txt
2. Display a Specific Number of Lines
To display a specific number of lines from the end of a file, use the -n
option followed by the number of lines:
tail -n 5 filename.txt
3. Display the Last Few Bytes of a File
To display the last few bytes of a file, use the -c
option followed by the number of bytes:
tail -c 20 filename.txt
4. Monitor a File in Real-Time
To monitor a file in real-time and display new lines as they are added, use the -f
(follow) option. This is particularly useful for watching log files:
tail -f filename.txt
5. Combine Options
You can combine options to tailor the output to your needs. For example, to display the last 3 lines and monitor a file in real-time:
tail -n 3 -f filename.txt
6. Display the Last 10 Lines of Multiple Files
To display the last 10 lines of multiple files, specify the file names separated by spaces:
tail file1.txt file2.txt