tail

file managementLinux/Unix
The tail command is one of the most frequently used commands in Linux/Unix-like operating systems. tail Output the last part of files

Quick Reference

Command Name:

tail

Category:

file management

Platform:

Linux/Unix

Basic Usage:

tail [options] [arguments]

Common Use Cases

    Syntax

    tail [OPTION]... [FILE]...

    Options

    Option Description
    -c, --bytes=NUM Output the last NUM bytes; or use -c +NUM to output starting with byte NUM
    -f, --follow[={name|descriptor}] Output appended data as the file grows; -f, --follow, and --follow=descriptor are equivalent
    -F Same as --follow=name --retry
    -n, --lines=NUM Output the last NUM lines, instead of the last 10; or use -n +NUM to output starting with line NUM
    --max-unchanged-stats=N With --follow=name, reopen a FILE which has not changed size after N iterations (default 5)
    --pid=PID With -f, terminate after process ID, PID dies
    -q, --quiet, --silent Never output headers giving file names
    --retry Keep trying to open a file if it is inaccessible
    -s, --sleep-interval=N With -f, sleep for approximately N seconds (default 1.0) between iterations
    -v, --verbose Always output headers giving file names
    -z, --zero-terminated Line delimiter is NUL, not newline
    --help Display help and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the tail command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Display the last 10 lines of a file (default) tail file.txt
    # Display the last 20 lines of a file tail -n 20 file.txt
    # Display the last 15 bytes of a file tail -c 15 file.txt
    # Monitor file for new lines (follow mode) tail -f log.txt
    # Advanced Examples Advanced
    # Monitor multiple files simultaneously tail -f log1.txt log2.txt # Display the last 10 lines of multiple files with headers tail file1.txt file2.txt file3.txt # Display all lines starting from line 20 tail -n +20 file.txt # Monitor a file that might be rotated (using inode) tail -F logfile.txt # Output appended data as the file grows, but exit when process with PID 1234 dies tail -f --pid=1234 logfile.txt # Display last 50 lines and continue monitoring tail -n 50 -f logfile.txt # Show last 10 lines with line numbers tail file.txt | nl # Monitor log file and highlight error patterns tail -f log.txt | grep --color=auto "ERROR" # View the last 1 KB of a file tail -c 1k large_file.txt # Show the last 10 lines of command output ls -la | tail # Monitor multiple log files and prefix with filename tail -f /var/log/*.log

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `tail` command is one of the most frequently used utilities in Linux and Unix-like operating systems, primarily for viewing the end portions of text files. It is particularly valuable for monitoring log files, as new entries are typically appended to the end of the file. By default, `tail` displays the last 10 lines of each specified file. However, it offers several options to customize its behavior: 1. **Line-based output**: The `-n` or `--lines` option allows you to specify the number of lines to display. For example, `tail -n 20 file.txt` shows the last 20 lines of the file. Interestingly, you can also use `tail -n +20 file.txt` to display all lines starting from line 20 to the end of the file. 2. **Byte-based output**: The `-c` or `--bytes` option lets you specify the number of bytes to display from the end of the file. This is useful when dealing with binary files or when you need precise control over how much data is displayed. 3. **Follow mode**: One of the most powerful features of `tail` is the `-f` or `--follow` option, which causes `tail` to continuously monitor the file and display new lines as they are appended. This is invaluable for real-time log monitoring. For example, `tail -f /var/log/syslog` will continuously display new system log entries as they occur. 4. **Smart follow with -F**: The `-F` option combines `--follow=name` with `--retry`, which means it will continue trying to read from the file even if it is inaccessible (e.g., during log rotation). This makes it more robust for monitoring logs that might be rotated or recreated. 5. **Multiple file monitoring**: `tail` can monitor multiple files simultaneously, displaying each new line with a header indicating which file it came from. This is useful for watching several logs at once. 6. **Process binding**: With the `--pid` option, `tail -f` will terminate when the specified process dies, which can be useful for monitoring logs of a specific application only while it's running. The `tail` command is often combined with other commands in pipelines. Common combinations include: - `tail -f log.txt | grep ERROR` to monitor a log file and only display lines containing "ERROR" - `tail -n 100 log.txt | less` to view the last 100 lines of a file in a pager - `tail -f multiple_files.log | tee -a saved_log.txt` to monitor files while also saving the output In system administration and debugging, `tail` is an indispensable tool for quickly checking recent log entries without having to open the entire file, which could be very large in the case of busy system logs.

    Related Commands

    These commands are frequently used alongside tail or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the tail command works in different scenarios.

    $ tail
    View All Commands