Display the Last Lines of a Text File with tail in Linux
Learn how to use Linux tail to display the last lines of text files, choose a line count, and monitor growing log files with follow mode.
The tail command displays lines from the end of a text file. It is especially useful for checking recent events, errors, and activity recorded in a log file. A log file is a text file that records messages or activity from a system or application.
By default, tail writes its result to standard output, which normally means your terminal. The command works with plain-text file content and does not interpret the entries as structured records.
Prerequisites
- Opening and using a Linux terminal
- Understanding absolute file paths
- Basic awareness of text files and log files
- Basic command syntax with options and arguments
Display the Last 10 Lines
The basic syntax is:
tail FILE
A line is a newline-separated record in a text file. When no line-count option is supplied, tail prints the last 10 lines.
tail /var/log/messages
This command displays the final 10 lines of /var/log/messages, when that file exists and is readable on the system. These lines may contain the most recent system messages, warnings, or other recorded activity.
Select a Specific Number of Ending Lines
Use the -n option to choose how many ending lines to print. The standard form is:
tail -n NUMBER FILE
For example, to display only the last five lines:
tail -n 5 /var/log/messages
The command prints the five most recent lines and then exits. The explicit -n form is recommended because it makes the requested line count clear. Many implementations also accept the compact form:
tail -5 /var/log/messages
Follow a Growing File
The -f option enables follow mode. In this mode, tail remains active after showing the current ending lines and prints lines appended after the command starts.
tail -f /var/log/messages
This is useful for live log monitoring. For example, you can run the command while an application is processing a request and watch for new status messages or errors.
Follow mode continues until you interrupt it. Press Ctrl+C to stop the command and return to the shell prompt.
Show Existing Lines Before Following
Combine -n and -f to display a chosen number of existing lines and then monitor new content:
tail -n 5 -f /var/log/messages
This displays five existing ending lines, then prints each new line as it is appended to the file.
Common tail Command Forms
| Command form | Purpose | Result |
|---|---|---|
tail FILE | Display the default ending portion | Prints the last 10 lines, then exits |
tail -n NUMBER FILE | Select the number of ending lines | Prints the specified number of lines, then exits |
tail -f FILE | Follow a growing file | Prints current ending lines and newly appended lines until interrupted |
tail -n NUMBER -f FILE | Show selected existing lines and follow the file | Prints the specified existing lines, then monitors new lines |
Reading tail Output Safely
A finite command such as tail -n 5 FILE prints a result and exits. A command using -f continuously monitors the file, so the terminal remains occupied while follow mode is running.
The output depends on both the file contents and your permissions. A file may exist but still be unreadable by your current user. Use only files you are authorized to inspect, and obtain administrative access according to local system policy when necessary.
Troubleshooting
File does not exist
If tail reports that the file does not exist, the path or filename may be incorrect, or the log may be stored elsewhere on that Linux distribution. Verify the path and inspect the relevant log directory before trying again.
tail /path/to/file
Permission denied
A permission error means your current user cannot read the selected file. Try a file you are allowed to read, or obtain appropriate administrative access according to local policy.
tail -f appears to do nothing
Follow mode does not repeatedly redraw the screen. It prints new output only when new lines are appended. No visible output may mean that the file has not changed, or that you selected the wrong log file. Wait for safe, expected activity or confirm the correct log source.
The terminal remains occupied
This is expected when using -f. Press Ctrl+C to interrupt tail and stop monitoring.
Exam-Relevant Notes
tailreads the end of a text file.- Without options, it prints the last 10 lines.
-nspecifies how many ending lines to print.-ffollows a file and displays newly appended lines.Ctrl+Cstops a running follow-mode command.- Log paths and filenames are not identical on every Linux distribution.
Related Linux Commands
To continue learning, see Linux command-line topics, learn how to determine a file's type, and review how to show the full path of a shell command.