tail - macOS


Overview

tail displays the last part of one or more files. It’s commonly used to view the end of a file (e.g., log files) or follow file updates in real-time.

Syntax

tail [options] [file1 [file2 ...]]

Options/Flags

  • -f, –follow: Continuously monitor files for changes and display new lines as they are added.
  • -n, –lines=NUM: Outputs the last NUM lines of each file. Default is 10.
  • -q, –quiet: Suppresses headers showing file names when multiple files are being displayed.
  • -v, –verbose: Includes file names in headers when multiple files are being displayed.
  • –help: Displays help information and exits.

Examples

  • Show the last 10 lines of a log file:

    tail /var/log/system.log
    
  • Follow a log file for real-time updates:

    tail -f /var/log/apache2/access.log
    
  • Display the last 20 lines of a file called myfile.txt:

    tail -n 20 myfile.txt
    
  • View the end of multiple files without file name headers:

    tail -q file1 file2 file3
    

Common Issues

  • Ensure files have the necessary permissions to be read.
  • Avoid using pipes (|) before tail as it can interfere with real-time monitoring.
  • If tail -f stops displaying output, the file may have been truncated or deleted.

Integration

  • Use tail with grep to filter output based on a pattern:

    tail -f /var/log/system.log | grep "error"
    
  • Combine tail with jq (JSON parser) to extract specific fields from JSON logs:

    tail -f /var/log/nginx/access.log | jq '.remote_addr'
    
  • head: Displays the beginning of files
  • cat: Concatenates and displays files
  • less: A more versatile command for viewing and navigating files
  • logrotate: Manages and rotates log files