In a Linux system, log data are normally added to the end of the file. You may require to see the new additions to the log file. Linux tail command is commonly used in this scenario to view those data.
In this tutorial, we learn about the Linux tail command and its useful options.
What is tail command
Linux tail command is a complementary part of head command. It prints the last ‘N’ number of lines from one or more files. By default, it prints the last 10 lines of a provided file.
Usually, in the data files such as error logs, access logs the data is added to the end of the file. The tail command helps to read last lines and monitor file changes in real-time.
How to use tail command
The basic syntax of Linux tail command:
tail [OPTION]... [FILE]...
For example, we have created a text file named ‘country.txt’. In this file, we copied the names of different countries in the world. To view the file content we may use cat command:
cat country.txt
Display the same file data using the tail command.
tail country.txt
When we use the tail command without any option, it prints only the last 10 lines of a file.
Tail Command Examples
Here we will check some of the commonly used tail commands with practical examples.
Print the specified last lines
Using the tail command, we can print the specified last lines of a file. To print the last 4 number of lines from a specified text file:
tail -n 4 country.txt
or
tail -4 country.txt
One practical example, to check the last 100 lines from /var/log/syslog
file, type:
tail -n 100 /var/log/syslog
Prints lines from start of a specific line
Using the ‘+’ option with a number, we can display the file content from the start of a specified line number till the end. For example to print all contents of the file named country.txt from line 1, type:
tail +1 country.txt
or
tail -n +1 country.txt
The following command prints file contents from line 4:
tail +4 country.txt
This comes helpful when you have a large file and want to print from a particular line number.
Print last specified bytes
Tail command is useful to display the last specified number of characters or bytes from a file. For example, to print the last 6 characters or bytes from a file named country.txt
, type:
tail -c -6 country.txt
or
tail -c 6 country.txt
With the positive number, it prints file content after skipping that much number of bytes from the start of the file.
tail -c +6 country.txt
tail command and multiple files
You can use multiple files with tail command. By default, if you pass multiple files it prints the file names in between.
tail food.txt country.txt
To ignore filenames, use -q
option when using more than one file.
tail -q food.txt country.txt
This prints the last 10 lines of both files without displaying the filenames.
Monitor files real-time
Monitoring the new text entries especially logs files is one of the best use of the tail command. Use the tail command followed by f
option to track files. By default it tail -f command shows the last 10 lines and the console will show the new lines when the file gets updated.
For example to track /var/log/syslog
file in real-time, type:
tail -f /var/log/syslog
You need to press the interrupt key to return back to the command prompt.
Use tail command with other commands
We can also use the Linux tail command in conjunction with other Linux commands. For example, you can use the sort command piped with the tail command:
tail –n 5 country.txt | sort
This prints the last 5 lines and sort them alphabetically.
You may also use the tail command along with head command:
tail –n +5 country.txt | head –n 2
This is another useful example where we can combine tail command along with nl command:
nl coountry.txt | tail -7
This prints the last specified number of lines with line numbers:
Tail Commands Options
Some of the useful options of tail command are:
Options | Description |
---|---|
-n Num | Prints the last Num lines. |
-c Num | Print the last Num bytes |
-q | Hide the headers of a given file names |
-f | Display appended data as the file grows |
-v | Prints the specified file names at the beginning of the output |
--version | Prints the installed version information |
Conclusion
In this tutorial, we learned the basic functionality of tail command and how it can be used to show the contents from the end of a file. Tail command has very few options which we have explained with examples.
Comments