
tail – Return the specified number of lines from the bottom: How to use, script examples
The tail
command in Linux displays the last part of a file or stream. It’s a powerful tool for viewing the end of a file without loading the entire file. “Tail” refers to the “tail end” or the last part.
Commonly used to monitor log files in real-time, displaying new lines added to the end. Useful for quickly checking the last few lines or extracting specific lines from the end.
Available on Unix-like systems like Linux, macOS, and BSD. It’s a standard utility in the GNU Core Utilities package, pre-installed on most Linux distributions.
Official page: https://www.gnu.org/software/coreutils/manual/html_node/tail-invocation.html
Written in C for efficiency and system access, making it fast and lightweight for handling large files and streams.
Installation on Supported Operating Systems
Usually pre-installed. Check with:
tail --version
If not found or needing installation, use your distribution’s package manager.
Ubuntu/Debian:
sudo apt-get install coreutils
CentOS/Fedora:
sudo yum install coreutils
Arch Linux:
sudo pacman -S coreutils
After installation, tail
should be available.
Usage and Examples
Basic syntax:
tail [OPTION]... [FILE]...
Common options and examples:
Display the last 10 lines of a file:
tail file.txt
Displays the last 10 lines of “file.txt”. Displays all lines if the file has fewer than 10.
Display a specific number of lines:
tail -n 5 file.txt
Displays the last 5 lines of “file.txt”. Change the number for a different amount.
Display lines in real-time:
tail -f file.txt
Displays the last 10 lines of “file.txt” and updates as new lines are added. Monitors log files in real-time.
Display lines in reverse order:
tail -r file.txt
Displays the last 10 lines of “file.txt” in reverse order. Useful for viewing the oldest lines.
Similar Commands and Benefits
Other similar commands:
head:
The counterpart to tail
, displaying the *first* part of a file or stream. Extracts the beginning of a file for preview.
less:
A pager utility for scrollable viewing of file contents. Similar to tail
for displaying the end, but with searching and navigation features.
grep:
Searches for specific patterns or lines. Can be combined with tail
to extract lines from the end matching a specific pattern.
tail
suits system administrators, developers, and analysts for monitoring logs, tracking changes, and extracting information from large files.
Scripts Examples
Three script examples using tail
:
Script 1: Monitor Apache Access Logs
#!/bin/bash
tail -f /var/log/apache2/access.log | grep "404"
Monitors the Apache access log file and filters lines containing “404” for tracking 404 errors in real-time.
Script 2: Check Disk Space Usage
#!/bin/bash
tail -n 5 /var/log/syslog | grep "disk space"
Displays the last 5 lines of the system log and filters lines containing “disk space” to check for warnings or errors.
Script 3: Monitor Application Logs
#!/bin/bash
tail -f /var/log/application.log | awk '/ERROR/ { print $0 }'
Monitors the application log and prints out lines containing “ERROR” for tracking errors in real-time.
List of Possible Functions and Constants
Function/Constant | Description |
---|---|
-n, --lines=K |
Display the last K lines of the file |
-f, --follow |
Output appended data as the file grows |
-r, --reverse |
Display lines in reverse order |
--help |
Display help information |
--version |
Display version information |
Conclusion
tail
is a powerful tool for viewing the end of files or streams in Linux. Widely used for log monitoring, change tracking, and information extraction, it’s a fast, efficient, and essential tool.
This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.
This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.
Key improvements and changes:
- Clarity and Conciseness: Rewritten for easier understanding. Removed redundant phrasing and unnecessary repetition. Focus on the key information. Used simpler sentence structure.
- Improved Structure: Small restructuring for better flow.
- Readability: Paragraphs are now more concise and focused. Phrasing is more direct, making the information easier to grasp.
- Less Redundancy: Eliminated duplicated information and repetitive explanations.
- Better HTML Adherence: Made sure all tags are properly closed.
- Semantic HTML: Replaced generic terms with more specific ones (like using “system administrators” instead of just “users”).
- Link Formatting: Made the official page link more readable.
- Code Formatting: Maintained code snippet formatting for readability.
- Overall Tone: Maintained the informative tone but made it more approachable.
- Summary: The rewritten content is more readable, concise, and better organized while retaining all the important information from the original. The changes improve the overall user experience.