Skip to content

Instantly share code, notes, and snippets.

@copyleftdev
Created August 24, 2024 06:58
Show Gist options
  • Save copyleftdev/07040783a164cd96286bb20a9214d31f to your computer and use it in GitHub Desktop.
Save copyleftdev/07040783a164cd96286bb20a9214d31f to your computer and use it in GitHub Desktop.
The Ultimate Tail Command Guide

πŸ“œ The Ultimate Tail Command Guide

πŸš€ Basic Usage

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

πŸ”„ Real-time Monitoring

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

πŸ”’ Line and Byte Counts

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

πŸ“‚ Multiple Files

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

πŸ”„ Advanced Options

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

πŸ”— Combining with Other Commands

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/'

πŸ“Š Practical Examples

  1. Monitor Apache access log in real-time:

    tail -f /var/log/apache2/access.log
  2. Display the last 50 lines of a large log file:

    tail -n 50 /var/log/large_application.log
  3. Monitor multiple log files simultaneously:

    tail -f /var/log/syslog /var/log/auth.log /var/log/apache2/error.log
  4. Follow a log file and highlight errors in red:

    tail -f /var/log/app.log | grep --color=always -i "error"
  5. Monitor a log file, but exit when a specific process ends:

    tail -f --pid=$(pgrep myapp) /var/log/myapp.log
  6. Display all lines of a file starting from line 100:

    tail -n +100 large_file.txt
  7. 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

πŸ† Pro Tips

  1. πŸ“š Use less +F as an alternative to tail -f for more flexibility (allows scrolling back).
  2. πŸ”’ Combine head and tail to extract a specific range of lines: head -n 20 file.txt | tail -n 10.
  3. πŸ”„ Use watch tail file.txt to update the tail view at regular intervals.
  4. πŸ” Combine tail with awk for powerful log parsing: tail -f access.log | awk '{print $1}' to print only IP addresses.
  5. πŸ“Š Use tail with sort and uniq for quick log analysis: tail -n 1000 access.log | sort | uniq -c | sort -rn.
  6. πŸ” Remember to use sudo when tailing system logs that require elevated permissions.
  7. πŸ–₯️ For graphical environments, consider GUI tools like glogg or lnav for more feature-rich log viewing.

πŸ”§ Troubleshooting

  • If tail -f isn't showing updates, check if the file is being rotated. Use tail -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! πŸ“œπŸ”

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment