tail -f: watch a log file live (and -F for rotated logs)
Use tail -f to follow a log as it grows, tail -n to see more lines, and tail -F so it keeps working when the log file is rotated.
tail prints the last 10 lines of a file. Add -f and it keeps running, printing new lines as they are written, which is how you watch a log while you reproduce a problem.
Watch a log while you test
- Run tail -f /var/log/nginx/error.log (use your own log path).
- Reproduce the problem in your browser or app.
- Read the new lines as they appear.
- Press Ctrl+C to stop watching.
Commands you can copy
tail -f /var/log/nginx/error.logtail -n 50 -f app.logtail -F app.logtail -n 100 app.logtail -n +20 app.logtail -f access.log error.logjournalctl -u myapp -ftail options explained
- -f vs -F
- With no argument, -f follows the open file (the file descriptor), so it keeps following the file even after it is renamed. -F is the same as --follow=name --retry: it follows the file by name and keeps trying if the file is missing, which suits rotated logs.
- -n N and -n +N
- -n 50 prints the last 50 lines instead of 10. -n +20 starts printing at line 20, counted from the start of the file.
- The log stops updating after rotation
- The old file was renamed and tail is still reading it. Restart with tail -F. The nginx commands page shows how to reopen logs.
- No such file or directory
- The log path is wrong or the program has not created it yet. -F waits for it to appear.
- Permission denied
- Many logs under /var/log are readable only by root or a group. Use sudo, or add your user to that group.
Questions about tail -f
- How do I stop tail -f?
- Press Ctrl+C.
- What is the default number of lines?
- 10 lines of each file.
- What does tail -F do?
- It follows the file by name and retries if the file is inaccessible, so it keeps working through log rotation.
- How do I follow a service log on systemd?
- Use journalctl -u servicename -f.