tail


tail (display last part of files)

The tail command is the counterpart to head and is used to output the final portion of files. Its most common use is to view the most recent entries in log files or other data that is appended over time.


Example 1: Display the last 10 lines of a file

# Show the default last 10 lines of the system messages log
tail /var/log/messages

Explanation: By default, tail reads a file and prints its final 10 lines. This is the quickest way to check the most recent activity in a log file.


Example 2: Specify the number of lines to display

# Show the last 20 lines from a log file
tail -n 20 /var/log/secure

Explanation: Using the -n option (or just -<number>) allows you to specify exactly how many lines from the end of the file you wish to see.


Example 3: Display the last few bytes of a file

# Show the last 100 bytes of a file
tail -c 100 data.bin

Explanation: The -c option switches tail to byte mode instead of line mode. This command will output the final 100 bytes of the file.


Example 4: Displaying tails of multiple files

# Show the last 5 lines of both the access and error logs
tail -n 5 /var/log/httpd/access_log /var/log/httpd/error_log

Explanation: When you provide multiple filenames, tail will display the last few lines from each one. It separates the output with a header indicating the filename.


Example 5: Using tail with a pipe

# List all files in a directory and show only the last 5 entries
ls | tail -n 5

Explanation: tail can be combined with other commands to limit output. This example shows just the last five files from the output of the ls command.


Example 6: Display lines starting from a specific line number

# Display the entire contents of the file starting from line 50
tail -n +50 /etc/services

Explanation: Using a + with the number in the -n option changes its meaning. It tells tail to start printing from that specific line number and continue all the way to the end of the file.


Example 7: Quiet mode for multiple files

# Show the last 5 lines of several files without the "==> filename <==" headers
tail -q -n 5 *.conf

Explanation: The -q (quiet) option prevents tail from printing the identifying headers when processing multiple files, resulting in a continuous block of output.