Linux – tail Command

The tail command is used to display the last few lines of a file. By default, it shows the last 10 lines, but you can customize the number of lines to display. This command is useful for viewing the most recent entries in log files or quickly checking the end of a file.



Tutorials dojo strip

Initial Example

Using tail to display the last 10 lines of a file called example.txt:

tail example.txt




TAIL Parameters

ParameterDescription
-c, --bytes=[+]NUMDisplay the last NUM bytes; use -c +NUM to begin from byte NUM of each file.
-f, --follow[={name|descriptor}]Continuously show appended data as the file grows; defaults to descriptor when no option is specified.
-FEquivalent to --follow=name --retry; useful for keeping track of files that may be temporarily inaccessible.
-n, --lines=[+]NUMDisplay the last NUM lines instead of the default 10; use -n +NUM to skip the first NUM-1 lines.
--max-unchanged-stats=NWith --follow=name, reopen a file if it hasn’t changed size after N (default 5) iterations; this is useful for rotated log files.
--pid=PIDWith -f, stop showing data after the process with ID PID terminates; can monitor multiple PIDs.
-q, --quiet, --silentSuppress file name headers in the output.
--retryKeep attempting to open a file if it is initially inaccessible.
-s, --sleep-interval=NWith -f, pause for approximately N seconds (default 1.0) between iterations; ensures process checks every N seconds with --pid.
-v, --verboseAlways show file name headers in the output.
-z, --zero-terminatedUse NUL character as the line delimiter instead of newline.
--helpDisplay help information and exit.
--versionShow version information and exit.




Examples

1. Display the Last 10 Lines of a File

To view the last 10 lines of a file (default behavior):

tail filename.txt

2. Display a Specific Number of Lines

To display a specific number of lines from the end of a file, use the -n option followed by the number of lines:

tail -n 5 filename.txt

3. Display the Last Few Bytes of a File

To display the last few bytes of a file, use the -c option followed by the number of bytes:

tail -c 20 filename.txt

4. Monitor a File in Real-Time

To monitor a file in real-time and display new lines as they are added, use the -f (follow) option. This is particularly useful for watching log files:

tail -f filename.txt

5. Combine Options

You can combine options to tailor the output to your needs. For example, to display the last 3 lines and monitor a file in real-time:

tail -n 3 -f filename.txt

6. Display the Last 10 Lines of Multiple Files

To display the last 10 lines of multiple files, specify the file names separated by spaces:

tail file1.txt file2.txt




Linux Playground

Scroll to Top