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

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

  • Osx
  • December 25, 2023

The ’tail’ command is a useful tool that allows users to display the last part of a file. It can be used to conveniently view the contents of files without having to open the entire file. ’tail’ is particularly helpful when dealing with large log files or when monitoring the output of a continuously updated file.

Use case 1: Show last ‘count’ lines in file

Code:

tail -n 8 path/to/file

Motivation:

Sometimes, we may only need to view the last few lines of a file, especially when dealing with log files or when trying to find the most recent entries in a document.

Explanation:

The ‘-n’ option in the command specifies the number of lines to display from the end of the file. In this case, ‘8’ is the number of lines to display.

Example output:

Line 1
Line 2
..
Line 8

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

Code:

tail -n +8 path/to/file

Motivation:

There may be instances where we want to print a file starting from a specific line number or exclude the first few lines of a file from the output.

Explanation:

The ‘-n’ option, when given a plus sign followed by a number (e.g., ‘+8’), specifies the line number to begin printing from. In this case, it starts printing from line 8.

Example output:

Line 8
Line 9
Line 10
..

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

Code:

tail -c 8 path/to/file

Motivation:

In some situations, we may need to view the last few bytes of a file instead of lines. This can be useful when dealing with binary files or when examining the file’s metadata.

Explanation:

The ‘-c’ option specifies the number of bytes to display from the end of the file. In this case, ‘8’ represents the number of bytes to display.

Example output:

Last 8 bytes of the file

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

Code:

tail -f path/to/file

Motivation:

When working with continuously updated files, such as log files or real-time data streams, it is often necessary to continuously monitor the output for new updates. The ‘-f’ option allows us to accomplish this without the need for constantly running the ’tail’ command.

Explanation:

The ‘-f’ option stands for “follow.” It prints the last few lines of the file and then waits for new lines to be appended to the file. It continuously refreshes the output, displaying any new lines as they are written.

Example output:

Line 1
Line 2
..
Line n

[Output stays continuously updated]

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

Code:

tail -F path/to/file

Motivation:

In some cases, the file being monitored by ’tail’ may become inaccessible or non-existent temporarily (e.g., during log rotation). We may still want ’tail’ to continue trying to read the file once it is accessible again without having to manually restart the command.

Explanation:

The ‘-F’ option is similar to ‘-f’, but it works even if the file is renamed or deleted temporarily. When the file becomes available again, ’tail’ resumes reading it. It is especially handy when dealing with log files that are periodically rotated.

Example output:

Line 1
Line 2
..
Line n

[Output stays continuously updated after file becomes accessible again]

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:

In situations where we want to continuously monitor the last few lines of a file while simultaneously refreshing the output at regular intervals, this use case comes in handy.

Explanation:

The ‘-s’ option specifies the number of seconds to wait between each refresh of the output. In this case, ‘10’ represents the number of seconds. By combining ‘-n’, ‘-s’, and ‘-f’, we can monitor the last ‘count’ lines of a file continuously, with a specified refresh interval.

Example output:

Line 1
Line 2
..
Line 8

[Output refreshes every 10 seconds with the latest lines displayed]

Conclusion:

The ’tail’ command is a versatile tool for conveniently viewing the last part of a file. It offers various options to cater to different use cases, such as displaying lines or bytes, following file updates, and even handling file inaccessibility. By mastering the ’tail’ command, users can efficiently analyze log files, track changes in real-time data, and monitor file updates with ease.

Tags :

Related Posts

How to use the command lsscsi (with examples)

How to use the command lsscsi (with examples)

The lsscsi command is used to list SCSI devices (or hosts) and their attributes.

Read More
How to use the command 'jest' (with examples)

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

Jest is a zero-configuration JavaScript testing platform used to run tests and monitor changes in JavaScript files.

Read More
How to use the command 'pio org' (with examples)

How to use the command 'pio org' (with examples)

The pio org command is used to manage PlatformIO organizations and their owners.

Read More