Tail Command Examples
Understanding the Tail Command
The tail command is a fundamental utility in Unix-like
operating systems, primarily used to display the last part of a
file. It's incredibly useful for monitoring log files in real-time,
checking recent entries, or quickly inspecting the end of large text
files without loading the entire content into memory. This tool
allows developers to efficiently manage and analyze file outputs
directly from the command line.
Basic Tail Command Usage
The simplest form of the tail command displays the last
10 lines of a specified file. This is often sufficient for a quick
look at recent activity.
# To show the last 10 lines of <file>:
tail <file>
Displaying a Specific Number of Lines
You can precisely control how many lines are displayed from the end
of the file using the -n option followed by the desired
number.
# To show the last <number> lines of <file>:
tail -n <number> <file>
Showing Lines from a Specific Starting Point
Conversely, you can use -n +<number> to display
all lines starting from a particular line number to the end of the
file. This is useful for viewing content from a specific point
onwards.
# To show the last lines of <file> starting with <number>:
tail -n +<number> <file>
Viewing the Last Bytes of a File
If you need to inspect the end of a file based on byte count rather
than line count, the -c option is available.
# To show the last <number> bytes of <file>:
tail -c <number> <file>
Real-time File Monitoring with Tail -f
One of the most powerful features of tail is its
ability to monitor files in real-time. The -f (follow)
option keeps the command running and continuously outputs new lines
as they are appended to the file. This is indispensable for live log
analysis.
# To show the last 10 lines of <file> and to wait for <file> to grow:
tail -f <file>