Bashtail Command - Display Last Part of Files
Using thetail Command
Thetail command is used to display the last part of files.
It's particularly useful for viewing the end of log files or any file that is being updated in real-time.
Syntax
The basic syntax of thetail command is:
tail [OPTION]... [FILE]...Example
tail logfile.txtline 91line 92line 93line 94line 95line 96line 97line 98line 99line 100Options
Thetail command has several options to customize its behavior:
-n [number]: Display the last [number] lines of the file.-f: Follow the file as it grows, useful for monitoring log files.-c [number]: Display the last [number] bytes of the file.--pid=[pid]: Terminate after the process with the given PID dies.--retry: Keep trying to open a file even if it is inaccessible.
Option: -n [number]
The-n option allows you to specify the number of lines to display from the end of the file.
By default,tail shows the last 10 lines.
Example: Display Last 5 Lines
tail -n 5 logfile.txtline 96line 97line 98line 99line 100Option: -f
The-f option is used to follow a file as it grows, which is particularly useful for monitoring log files in real-time.
Example: Follow Log File
tail -f logfile.txtline 91line 92line 93line 94line 95line 96line 97line 98line 99line 100line 101Option: -c [number]
The-c option allows you to display the last [number] bytes of a file instead of lines.
Example: Display Last 20 Bytes
tail -c 20 logfile.txtend of logfile.txtOption: --pid=[pid]
The--pid option terminates tailing after the process with the given PID dies. This is useful for stopping the tail operation when a related process ends.
Example: Terminate After Process Ends
tail -f --pid=1234 logfile.txtline 91line 92line 93...Option: --retry
The--retry option makestail keep trying to open a file even if it is inaccessible. This is useful for files that may be temporarily unavailable.
Example: Retry Opening File
tail --retry -f logfile.txttail: cannot open 'logfile.txt' for reading: No such file or directoryline 91line 92line 93...Use Cases
Common scenarios where thetail command is beneficial include:
- Monitoring server logs to detect issues in real-time.
- Checking the latest entries in a continuously updated file.
- Debugging applications by reviewing recent log entries.

