How to use the command 'tail' (with examples)

How to use the command 'tail' (with examples)

  • Osx
  • December 17, 2024

The tail command is an essential utility in Unix and Unix-like operating systems, including Linux and macOS. It is primarily used to display the last part of a file, making it invaluable for monitoring logs or outputs from processes that append information over time. This command allows users to view the last several lines or bytes of a file and provides options to keep monitoring a file for new content, which is particularly useful for real-time diagnostics and log file monitoring.

Use case 1: Display the last ‘count’ lines in a file

Code:

tail -n 8 path/to/file

Motivation:

This command is particularly useful when you’re interested in looking at the most recent entries in a log file, such as error logs or system logs, to diagnose issues. By specifying the last n lines, you can focus on recent activity without wading through potentially large files.

Explanation:

  • tail is the command being used.
  • -n is an option that specifies the number of lines to be displayed.
  • 8 is the count of the lines you wish to see from the end of the file.
  • path/to/file is the path to the file you want to check.

Example Output:

If path/to/file contains:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
Line 12

The output will be:

Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
Line 12

Use case 2: Print a file from a specific line number

Code:

tail -n +8 path/to/file

Motivation:

There are times when you might want to examine a file starting from a specific point onward, rather than from the beginning or end. This command is beneficial for structured log files or datasets where you’re interested in a specific range of lines, skipping the initial section that may include headers or metadata.

Explanation:

  • tail is the command being used.
  • -n +8 signifies starting from the 8th line, showing all lines from that point to the end of the file.
  • + before 8 indicates the starting line number.
  • path/to/file is the path to the file being read.

Example Output:

Given the same file as before, the output will be:

Line 8
Line 9
Line 10
Line 11
Line 12

Use case 3: Print a specific count of bytes from the end of a given file

Code:

tail -c 8 path/to/file

Motivation:

This option is sought after when one needs to verify binary data or fetch a specific byte-length string from the end of a log entry or data stream. It can be very effective for examining file integrity or analyzing byte-level data in files.

Explanation:

  • tail is the command being used.
  • -c option specifies the number of bytes to be shown.
  • 8 is the count of bytes you want to display from the end of the file.
  • path/to/file is the path to the file you are inspecting.

Example Output:

Assuming path/to/file content as:

1234567890abcdef

The output would be:

8abcdef

Use case 4: Print the last lines of a given file and keep reading it until Ctrl + C

Code:

tail -f path/to/file

Motivation:

Widely utilized for live monitoring of log files, the -f option allows you to watch the ongoing changes in a file. This action is especially popular among system administrators who need to observe real-time logs for changes, updates, or errors as processes run.

Explanation:

  • tail is the command being used.
  • -f stands for follow, which keeps the command active, reading new lines as they are appended to the file.
  • path/to/file is the path to the target file.

Example Output:

Suppose path/to/file initially contains:

Current log begins

Upon new entries, the output in the terminal would dynamically update as follows:

Current log begins
New log entry 1
New log entry 2

Use case 5: Keep reading file until Ctrl + C, even if the file is inaccessible

Code:

tail -F path/to/file

Motivation:

Sometimes files get rotated, replaced, or temporarily unavailable, especially in a logging environment where logs are periodically archived and reset. The -F option ensures that once the file becomes available again, monitoring resumes seamlessly without restarting the tail command.

Explanation:

  • tail is the command being used.
  • -F option not only follows the file like -f, but also enables tailing the file when it becomes inaccessible or replaced.
  • path/to/file is the path to the monitored file.

Example Output:

If the file begins as:

Starting log process

And then it gets temporarily inaccessible or replaced before resuming with:

Process resumed with new data

The output will accommodate the availability phases naturally:

Starting log process
Process resumed with new data

Use case 6: Show last ‘count’ lines in ‘file’ and refresh every ‘seconds’ seconds

Code:

tail -n 8 -s 10 -f path/to/file

Motivation:

This combined functionality can be critical for system performance monitoring, illustrating updates periodically. Instead of constant file syncing, this reduces resource overhead by checking changes at set intervals.

Explanation:

  • tail is the command being used.
  • -n 8 specifies the number of lines to display.
  • -s 10 sets a delay of 10 seconds between repeated reads while in follow mode.
  • -f keeps the command active to track new file entries.
  • path/to/file is the path to the file in question.

Example Output:

Initially, path/to/file:

Output generated

Every 10 seconds, the terminal would check and exhibit new lines:

Output generated
Interval 1 reached
Interval 2 reached

Conclusion:

The tail command is a powerful utility in Unix/Linux environments, enabling effective file examination, particularly for logs or data files that are continually updated. Whether you need to view a specific segment of a file, monitor real-time updates, or track file rotation, tail offers versatile options to meet diverse file viewing requirements.

Tags :

Related Posts

How to use the command 'wireplumber' (with examples)

How to use the command 'wireplumber' (with examples)

WirePlumber is a powerful and modular session/policy manager for PipeWire, providing a high-level library that simplifies interaction with PipeWire’s API.

Read More
How to use the command 'systemd-confext' (with examples)

How to use the command 'systemd-confext' (with examples)

systemd-confext is a command-line tool on Linux systems that allows users to extend or overlay configurations within the /etc directory.

Read More
Mastering the Use of the Command 'gcpdiag' (with examples)

Mastering the Use of the Command 'gcpdiag' (with examples)

gcpdiag is a powerful troubleshooting and diagnostics tool specifically designed for Google Cloud Platform (GCP).

Read More