How to Use the Tail Command in Linux
The tail
command stands for “tail of the file,” and as the name suggests, it’s primarily used to view the last part of files. Whether you’re monitoring log files or tracking real-time data changes, tail
is your go-to utility.
The command is often used in conjunction with other Linux commands like grep
for searching, awk
for text processing, and |
(pipe) for chaining multiple commands together. For example, you might use tail -f /var/log/syslog | grep "error"
to monitor system logs for errors in real-time.
General syntax for tail
command:
$ tail [OPTION...] [FILE...]
1. Display the exact number of lines
tail -n [number] [file]
Using this option allows you to get the tail
command to produce output that will display a certain number of lines in a file.
Example:
Suppose you have a text file named example.txt
with the following content:
Line 1: This is the first line. Line 2: This is the second line. Line 3: This is the third line. Line 4: This is the fourth line. Line 5: This is the fifth line. Line 6: This is the sixth line. Line 7: This is the seventh line. Line 8: This is the eighth line. Line 9: This is the ninth line. Line 10: This is the tenth line.
If you want to display the last 3 lines of this file, you would use the following command:
tail -n 3 example.txt
This will show you the last 3 lines of the example.txt
file.
Line 8: This is the eighth line. Line 9: This is the ninth line. Line 10: This is the tenth line.
2. Display lines starting from a specific line number
tail +[number] [file]
The command with the +
sign outputs data starting from the specified line number.
Example:
Let’s say you have a text file named example.txt
with the following content:
Line 1: This is the first line. Line 2: This is the second line. Line 3: This is the third line. Line 4: This is the fourth line. Line 5: This is the fifth line. Line 6: This is the sixth line. Line 7: This is the seventh line. Line 8: This is the eighth line. Line 9: This is the ninth line. Line 10: This is the tenth line.
If you want to display the content starting from line 5 to the end of the file, you can use the following command:
tail +5 example.txt
The output will be:
Line 5: This is the fifth line. Line 6: This is the sixth line. Line 7: This is the seventh line. Line 8: This is the eighth line. Line 9: This is the ninth line. Line 10: This is the tenth line.
3. Display multiple files
tail [file1] [file2]
Use this command to display the data of multiple files at the same time.
Example 1: Viewing the Last 10 Lines of a Single File
Let’s say you have a file named example.txt
with the following content:
Line 1: Hello Line 2: World Line 3: This Line 4: is Line 5: a Line 6: sample Line 7: text Line 8: file Line 9: for Line 10: demonstration Line 11: purposes Line 12: only
Running the command:
tail example.txt
Will output:
Line 3: This Line 4: is Line 5: a Line 6: sample Line 7: text Line 8: file Line 9: for Line 10: demonstration Line 11: purposes Line 12: only
Example 2: Viewing the Last 10 Lines of Multiple Files
Suppose you have another file named example2.txt
with the following content:
Line 1: Another Line 2: Example Line 3: File
You can view the last 10 lines of both example.txt
and example2.txt
by running:
tail example.txt example2.txt
This will output:
==> example.txt <== Line 3: This Line 4: is Line 5: a Line 6: sample Line 7: text Line 8: file Line 9: for Line 10: demonstration Line 11: purposes Line 12: only ==> example2.txt <== Line 1: Another Line 2: Example Line 3: File
The ==>
<==
notation is used to separate the output from different files.
4. Output a certain number of bytes
tail -c [bytes] [file]
To display a specific number of bytes in a text file, use the -c
option.
Example:
Let's say we have a file named sample.txt
with the following content:
This is the first line. This is the second line. This is the third line. This is the fourth line. This is the fifth line.
If we want to display the last 20 bytes of this file, we would use the following command:
tail -c 20 sample.txt
5. Use multiple commands at once
tail [file] | [other_command]
Use the tail
command with pipes |
to use it in conjunction with another command.
Example: Using tail
with grep
Let's say you have a log file called server.log
and you want to check the last 10 lines for any occurrences of the word "error
".
tail server.log | grep 'error'
Here's what happens:
tail server.log
reads the last 10 lines of the server.log file.- The output is then piped (
|
) to thegrep 'error'
command. grep 'error'
filters the lines to only show those that contain the word "error
".
6. Monitor files in real-time
tail -f [file]
The -f
option is used to track file changes. When new log entries are added to the log file, it updates the display in the terminal window.
Example:
Let's say you have a log file named application.log
that is being written to while an application is running. You want to monitor this log file for any new entries.
Open a Terminal Window, navigate to the directory where application.log
is located, i.e cd /path/to/directory
.
Run the tail -f
command:
tail -f application.log
After running this command, you'll see the last 10 lines of application.log
displayed in the terminal. The terminal will stay open, and any new lines added to application.log
will be displayed in real-time.
More Linux commands:
Directory Operations | rmdir · cd · pwd · exa · ls |
File Operations | cat · cp · dd · less · touch · ln · rename · more · head |
File System Operations | chown · mkfs · locate |
Networking | ping · curl · wget · iptables · mtr |
Search and Text Processing | find · grep · sed · whatis · ripgrep · fd · tldr |
System Information and Management | env · history · top · who · htop · glances · lsof |
User and Session Management | screen · su · sudo · open |