tail
Overview
The tail
command outputs the last part of files. It’s particularly useful for monitoring log files and viewing recent changes.
Syntax
tail [options] [file...]
Common Options
Option | Description |
---|---|
-n num |
Output last num lines |
-f |
Follow file growth |
-F |
Follow and retry if file inaccessible |
-c num |
Output last num bytes |
-q |
Never output headers |
-v |
Always output headers |
--pid=PID |
With -f, terminate after PID dies |
--retry |
Keep trying to open file |
--max-unchanged-stats=N |
Reopen file after N iterations |
Key Use Cases
- Monitor log files
- View recent changes
- Follow file updates
- Debug applications
- System monitoring
Examples with Explanations
Example 1: View End
tail file.log
Show last 10 lines
Example 2: Follow Updates
tail -f log.txt
Monitor file for new content
Example 3: Multiple Files
tail -n 5 file1 file2
Show last 5 lines of each file
Understanding Output
- Default: 10 lines
- With -f:
- Real-time updates
- With multiple files:
- ==> filename <== headers
- Error messages for:
- File not found
- Permission denied
- File rotation
Common Usage Patterns
Monitor logs:
tail -f /var/log/syslog
View recent changes:
tail -n 50 file
Follow multiple files:
tail -f file1 file2
Performance Analysis
- Efficient file following
- Memory usage control
- Inode monitoring
- File rotation handling
- Multiple file impact
Additional Resources
Best Practices
- Use -F for log monitoring
- Consider log rotation
- Set appropriate buffer size
- Use with grep for filtering
- Monitor resource usage