How to Use tail -f Command in Linux for Real-Time File Monitoring
Use the
tail -f command in Linux to continuously watch the end of a file as it grows, showing new lines in real time. This is useful for monitoring logs or output files that update frequently.Syntax
The basic syntax of the tail -f command is:
tail: The command to display the end of a file.-f: The option to follow the file, showing new lines as they are added.filename: The name of the file you want to monitor.
bash
tail -f filename
Example
This example shows how to use tail -f to watch a log file named system.log. As new lines are added to the file, they will appear on your screen immediately.
bash
tail -f system.logOutput
[new log lines appear here as system.log updates]
Common Pitfalls
Some common mistakes when using tail -f include:
- Trying to follow a file that does not exist, which will cause an error.
- Not having permission to read the file, resulting in a permission denied error.
- Using
tail -fon very large files without filters, which can flood your terminal.
To stop following the file, press Ctrl + C.
bash
tail -f nonexistent.log # Error: tail: cannot open 'nonexistent.log' for reading: No such file or directory # Correct usage: tail -f /var/log/syslog
Output
tail: cannot open 'nonexistent.log' for reading: No such file or directory
# Then running correct command shows live updates
Quick Reference
| Option | Description |
|---|---|
| -f | Follow the file as it grows, showing new lines in real time |
| -n | Show the last |
| --help | Display help information about tail command |
Key Takeaways
Use
tail -f filename to watch new lines added to a file in real time.Press Ctrl + C to stop following the file.
Ensure the file exists and you have read permission to avoid errors.
Combine
-f with -n to control how many lines you see initially.Ideal for monitoring log files or any file that updates continuously.