How to Print Last 10 Lines of Files in Linux

The tail command is an indispensable tool for system administrators and developers, particularly for log analysis and real-time monitoring of file changes.

Alby Andersen

The tail command in Linux displays the end of a text file or input stream, making it ideal for monitoring logs, checking recent data, or tracking real-time updates. By default, it shows the last 10 lines of a file, but you can adjust the number of lines or bytes. Its -f (follow) mode is particularly useful for live log monitoring. Below are practical examples to master tail:


Display the Last 10 Lines of a File

tail file.txt


Shows the default last 10 lines of file.txt.


Show a Specific Number of Lines

tail -n 20 file.txt


Displays the last 20 lines of the file.


Monitor Real-Time Updates in a Log

tail -f /var/log/syslog
  • -f (follow) keeps the output open and appends new lines in real time.
  • Press Ctrl+C to exit.

Track Logs Even After File Rotation

tail -F /var/log/nginx/access.log
  • -F (capital F) follows the file even if it’s recreated (e.g., log rotation).

Display Bytes Instead of Lines

# Display last 20 bytes of a file
tail -c 20 filename.txt

# Display last 1 kilobyte of a file
tail -c 1K large_file.dat

# Display last 1 megabyte of a file
tail -c 1M very_large_file.bin


Shows the last 200 bytes of the file.


View the End of Multiple Files

tail -v file1.txt file2.log
  • -v (verbose) adds filename headers for clarity.

Suppress Filename Headers

tail -q file1.txt file2.txt
  • -q (quiet) hides filenames when viewing multiple files.

Pipe Output from Another Command

ls -l /etc | tail -n 5


Lists files in /etc and shows the last 5 entries.


Save the Last Few Lines to a File

tail -n 10 app.log > last_errors.txt


Saves the last 10 lines of app.log into last_errors.txt.


Combine with head to Extract Middle Sections

head -30 file.txt | tail -n 10


Extracts lines 21–30 of file.txt (first 30 via head, then last 10 via tail).


Key Notes:

  • Real-Time Monitoring: Use -f for live logs (e.g., tail -f /var/log/syslog).
  • Log Rotation: Prefer -F over -f if logs are rotated (e.g., by logrotate).
  • Large Files: Efficient for massive files since it doesn’t load the entire content.
Share This Article