Command | Description | Example |
---|---|---|
tail file.txt |
π Display the last 10 lines of a file | tail /var/log/syslog |
tail -n 20 file.txt |
π’ Display the last 20 lines of a file | tail -n 20 /var/log/auth.log |
tail -c 100 file.txt |
π Display the last 100 bytes of a file | tail -c 100 /etc/passwd |
Command | Description | Example |
---|---|---|
tail -f file.txt |
π Follow the file in real-time | tail -f /var/log/apache2/access.log |
tail -F file.txt |
π Follow and retry if file is inaccessible | tail -F /var/log/app.log |
Command | Description | Example |
---|---|---|
tail -n +10 file.txt |
π Display all lines starting from line 10 | tail -n +10 /etc/services |
tail --lines=+10 file.txt |
π Same as above, long option | tail --lines=+10 /etc/services |
tail -c -100 file.txt |
π Display all but the last 100 bytes | tail -c -100 /var/log/syslog |
Command | Description | Example |
---|---|---|
tail file1.txt file2.txt |
π Display last 10 lines of multiple files | tail /var/log/syslog /var/log/auth.log |
tail -q file1.txt file2.txt |
π€« Quiet mode, don't display file headers | tail -q /var/log/syslog /var/log/auth.log |
Option | Description | Example |
---|---|---|
--pid=PID |
π Exit when the specified process ID dies | tail -f --pid=1234 /var/log/app.log |
--max-unchanged-stats=N |
π Reopen a file after N iterations if it hasn't changed | tail -f --max-unchanged-stats=5 /var/log/app.log |
--sleep-interval=N |
β° Sleep for N seconds between iterations | tail -f --sleep-interval=2 /var/log/app.log |
Command | Description | Example |
---|---|---|
tail -f file.txt | grep "error" |
π Follow file and filter for "error" | tail -f /var/log/syslog | grep "error" |
tail -f file.txt | sed 's/foo/bar/' |
π Follow file and replace "foo" with "bar" | tail -f /var/log/apache2/access.log | sed 's/GET/POST/' |
-
Monitor Apache access log in real-time:
tail -f /var/log/apache2/access.log
-
Display the last 50 lines of a large log file:
tail -n 50 /var/log/large_application.log
-
Monitor multiple log files simultaneously:
tail -f /var/log/syslog /var/log/auth.log /var/log/apache2/error.log
-
Follow a log file and highlight errors in red:
tail -f /var/log/app.log | grep --color=always -i "error"
-
Monitor a log file, but exit when a specific process ends:
tail -f --pid=$(pgrep myapp) /var/log/myapp.log
-
Display all lines of a file starting from line 100:
tail -n +100 large_file.txt
-
Monitor a log file and send an alert on error:
tail -f /var/log/app.log | grep --line-buffered "ERROR" | while read line; do echo "Alert: $line" | mail -s "App Error" admin@example.com; done
- π Use
less +F
as an alternative totail -f
for more flexibility (allows scrolling back). - π’ Combine
head
andtail
to extract a specific range of lines:head -n 20 file.txt | tail -n 10
. - π Use
watch tail file.txt
to update the tail view at regular intervals. - π Combine
tail
withawk
for powerful log parsing:tail -f access.log | awk '{print $1}'
to print only IP addresses. - π Use
tail
withsort
anduniq
for quick log analysis:tail -n 1000 access.log | sort | uniq -c | sort -rn
. - π Remember to use
sudo
when tailing system logs that require elevated permissions. - π₯οΈ For graphical environments, consider GUI tools like
glogg
orlnav
for more feature-rich log viewing.
- If
tail -f
isn't showing updates, check if the file is being rotated. Usetail -F
instead. - On some systems, very large files might cause issues. Use
tail -c
to start from the end of the file. - If you're not seeing expected output, ensure you have read permissions on the file.
Remember, tail
is a powerful tool for log analysis and file inspection. Use it responsibly, especially when working with sensitive log files. Happy tailing! ππ