The tail command is one option for viewing the contents of a file. By default, the tail command will display the last 10 lines in the provided file. You can also utilize it to view real-time changes on a file, such as checking the log files.
You will often utilize the tail with the head command. While the tail command shows the last lines in the file, the head command does the opposite. It shows the first 10 lines in the target file. How do you use the tail Linux command? Read on to find out!
The Syntax and Options to Use with the tail Command
The command uses the below syntax.
$ tail [option] [file]
Aside from understanding its syntax, the table below highlights some commonly used options with the command.
Option | Description |
-n | It sets the number of lines to include in the output. |
-c | It displays the output based on specified bytes instead of lines. |
-q | It removes the file names when displaying output. |
-v | It displays information when running the tail command. |
-s | It sets the wait time between iterations and is used alongside the -f option. |
Examples of How to Use the tail Command
Let’s explore how you can use the tail command for different needs.
1. Basic Usage of the tail Command
The primary way of using the command is to display the last 10 lines in a file. First, let’s run the cat command to display the file’s contents. You will notice that running the cat command will include all the lines in the output.
However, only the last lines are displayed if we run the tail Linux command.
$ tail names.txt
2. Show a Specific Number of Lines
Instead of displaying the last lines, you can specify how many lines to be displayed. The -n option will take the added number and run the tail command.
For instance, we are displaying only five lines with the below command.
$ tail -n 5 names.txt
3. Set the Start Position
With the tail command, you can define where to start printing the lines using the +n option. The command will note the start position and print all the other lines from that position to the end of the file.
To set our start position at line 12, our command would be as follows.
$ tail -n +12 names.txt
4. Use tail Command with Multiple Files
You can also display the contents of multiple files when running the command. Add the names of the target files and include any option.
We’ve displayed the last five lines for our two files.
$ tail -n 5 names.txt list.txt
5. tail Command Suppress Filename
In the previous example, the output shows the file names of all the files you’ve used. However, you can remove the file names using the -q or (quiet mode).
$ tail -q -n 5 list.txt names.txt
6. Redirect Output
When you want to save the output of the tail command, you can use the redirect option and specify the name for the output file.
$ tail -n 5 list.txt names.txt > tail.txt
Once the command executes, run the cat command to display the contents of the output file. It will contain the exact output you would get when running the tail command.
7. Display Output Based on Bytes
When you add the -c option, the tail command will display the last bytes instead of lines. The example demonstrates our results using the -c option with 30 bytes.
$ tail -c 30 names.txt
8. Display Last Lines in Real-time
You can monitor changes on a file by using the -f option. The command will open the target file and display any new lines as they appear at the bottom.
For this example, we’ve opened a file and replaced its content using the cat command.
The tail command will show all the newly added lines to that file.
$ tail -f names.txt
9. Add Verbose
Adding the -v option will display the file name at the top of the output.
Here’s an example.
$ tail -v names.txt
10. Using the tail Command with Other Commands
Combining the command with other commands is possible when performing tasks. For instance, the ls command lists the contents of a directory. You can couple it with the tail command to only show the last elements in the displayed list.
We’ve limited our output to four lines.
$ ls | tail -n 4
When working with a long file, the grep command will help search for text in the file. Moreover, adding the tail Linux command will limit the search to only the last 10 lines in the file.
Here’s an example.
$ tail /var/log/dpkg.log | grep amd
Conclusion
Knowing how to use the tail Linux command gives you an advantage when working with files. The tail command displays the last 10 lines in a file, and you can utilize its options to customize the output. We’ve discussed the tail command and given examples of using it.