How to use the command 'tail' (with examples)
The ’tail’ command is a utility in Unix-like operating systems that allows users to display the last part of a file. It is often used to monitor log files in real-time or to extract a specific portion of a file from the end.
Use case 1: Show last ‘count’ lines in file
Code:
tail --lines count path/to/file
Motivation: This use case is helpful when we want to examine the last few lines of a file, such as log files, to check for any recent updates or errors.
Explanation:
--lines count
: Specifies the number of lines to be displayed from the end of the file.
Example output:
If we run tail --lines 10 example.log
, it will display the last 10 lines of the ’example.log’ file.
Use case 2: Print a file from a specific line number
Code:
tail --lines +count path/to/file
Motivation: This use case is useful when we want to start printing a file from a specific line number, skipping the initial part.
Explanation:
--lines +count
: Specifies the starting line number from where the file should be printed.
Example output:
Running tail --lines +5 example.log
will print all the lines starting from the 5th line of the ’example.log’ file.
Use case 3: Print a specific count of bytes from the end of a given file
Code:
tail --bytes count path/to/file
Motivation: In some cases, we might want to extract a certain number of bytes from the end of a file. This use case allows us to do that.
Explanation:
--bytes count
: Specifies the number of bytes to be displayed from the end of the file.
Example output:
By running tail --bytes 100 example.txt
, it will display the last 100 bytes of the ’example.txt’ file.
Use case 4: Print the last lines of a given file and keep reading file until Ctrl + C
Code:
tail --follow path/to/file
Motivation: This use case is useful when we want to continuously monitor a file and display its last lines in real-time.
Explanation:
--follow
: Continuously monitors the file for any changes and keeps printing the latest lines as they are appended to the file.
Example output:
If we run tail --follow example.log
, it will continuously display the last lines of the ’example.log’ file until we interrupt it with Ctrl + C.
Use case 5: Keep reading file until Ctrl + C, even if the file is inaccessible
Code:
tail --retry --follow path/to/file
Motivation: Sometimes, a file may become inaccessible temporarily due to various reasons. This use case ensures that tail keeps trying to access the file, even if it fails initially.
Explanation:
--retry
: Causes tail to retry opening the file if it is inaccessible.
Example output:
Running tail --retry --follow example.log
will continuously try to open and read the ’example.log’ file, even if it is temporarily inaccessible. It will display the last lines once the file becomes accessible.
Use case 6: Show last ’num’ lines in ‘file’ and refresh every ’n’ seconds
Code:
tail --lines count --sleep-interval seconds --follow path/to/file
Motivation: This use case is helpful when we want to continuously monitor a file, displaying the last ’num’ lines with a specified refresh interval.
Explanation:
--sleep-interval seconds
: Specifies the number of seconds to wait before refreshing and displaying the latest lines.
Example output:
By running tail --lines 20 --sleep-interval 5 --follow example.log
, it will continuously display the last 20 lines of the ’example.log’ file, refreshing every 5 seconds.
Conclusion:
The ’tail’ command provides various useful functionalities for analyzing and monitoring files. Whether you want to view the last lines, continuously monitor changes, or extract a specific portion, ’tail’ has got you covered. With the examples provided above, you can now make the most out of this powerful command.