Monitoring log files in real time is a critical skill for system administrators. The tail and grep commands are your primary tools for this task.

Follow a Log File in Real Time

tail -f /var/log/syslog

Show Last N Lines

tail -n 100 /var/log/nginx/error.log

Filter Logs with grep

tail -f /var/log/nginx/access.log | grep "404"

Search Across Multiple Log Files

grep -r "ERROR" /var/log/

Case-Insensitive Search

grep -i "warning" /var/log/syslog

Show Context Around a Match

grep -C 3 "segfault" /var/log/syslog