Open In App

Tail command in Linux with examples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

It is the complementary of head command. The tail command, as the name implies, prints the last N number of data of the given input. By default, it prints the last 10 lines of the specified files. If more than one file name is provided then data from each file is preceded by its file name.

Syntax of Tail Command in Linux

tail [OPTION]... [FILE]...

Let us consider two files having a name state.txt and capital.txt containing all the names of the Indian states and capitals respectively.

cat state.txt

Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
Goa
Gujarat
Haryana
Himachal Pradesh
Jammu and Kashmir
Jharkhand
Karnataka
Kerala
Madhya Pradesh
Maharashtra
Manipur
Meghalaya
Mizoram
Nagaland
Odisha
Punjab
Rajasthan
Sikkim
Tamil Nadu
Telangana
Tripura
Uttar Pradesh
Uttarakhand
West Bengal

Without any option it display only the last 10 lines of the file specified.

Example:

tail state.txt


Here we will only get names of last 10 states after using tail command.

tail command in Linux

tail command in Linux

Options and Pratical Examples of Tail Command in Linux

1. `-n` num Option in Tail Command in Linux

Prints the last ‘num’ lines instead of last 10 lines. num is mandatory to be specified in command otherwise it displays an error. This command can also be written as without symbolizing ‘n’ character but ‘-‘ sign is mandatory.

tail -n 3 state.txt


or

tail -3 state.txt


85

Tail command also comes with an ‘+’ option which is not present in the head command. With this option tail command prints the data starting from specified line number of the file instead of end. For command:

tail +n file_name, data will start printing from line number ‘n’ till the end of the file specified.

tail +25 state.txt

86

tail +n option in Linux

2. `-c` num Option in Tail Command in Linux

Prints the last ‘num’ bytes from the file specified. Newline count as a single character, so if tail prints out a newline, it will count it as a byte. In this option it is mandatory to write -c followed by positive or negative num depends upon the requirement. By +num, it display all the data after skipping num bytes from starting of the specified file and by -num, it display the last num bytes from the file specified.

Note: Without positive or negative sign before num, command will display the last num bytes from the file specified.

With negative num

tail -c -7 state.txt

or

tail -c 7 state.txt

-c option in tail command in Linux

-c option in tail command in Linux (using negative)

With positive num

tail -c +263 state.txt

-c option in tail command in Linux (using positive)

-c option in tail command in Linux (using positive)

3. `-q` Option in Tail Command in Linux

It is used if more than 1 file is given. Because of this command, data from each file is not precedes by its file name.

But before lets see text inside capital.txt file.

cat capital.txt

Amaravati
Itanagar
Dispur
Patna
Raipur
Panaji
Gandhinagar
Chandigarh
Shimla
Srinagar (summer), Jammu (winter)
Ranchi
Bengaluru
Thiruvananthapuram
Bhopal
Mumbai
Imphal
Shillong
Aizawl
Kohima
Bhubaneswar
Chandigarh
Jaipur
Gangtok
Chennai
Hyderabad
Agartala
Lucknow
Dehradun
Kolkata

Without using -q option

tail state.txt capital.txt

Without using -q option in tail command in Linux

Without using -q option in tail command in Linux

With using -q option

tail state.txt -q capital.txt

With using -q option in tail command in Linux

With using -q option in tail command in Linux

4. `-f` Option in Tail Command in Linux

This option is mainly used by system administration to monitor the growth of the log files written by many Unix program as they are running. This option shows the last ten lines of a file and will update when new lines are added. As new lines are written to the log, the console will update with the new lines.

The prompt doesn’t return even after work is over so, we have to use the interrupt key to abort this command. In general, the applications writes error messages to log files. You can use the -f option to check for the error messages as and when they appear in the log file.

$ tail -f logfile

5. `-v` Option in Tail Command in Linux

By using this option, data from the specified file is always preceded by its file name.

tail -v state.txt

-v option in tail command in Linux

-v option in tail command in Linux

6. `–version` Option in Tail Command in Linux

This option is used to display the version of tail which is currently running on your system.

tail --version

To check version of tail command in Linux

To check version of tail command in Linux

Applications of tail Command in Linux

1. How to use tail with pipes(|):

The tail command can be piped with many other commands of the unix. In the following example output of the tail command is given as input to the sort command with -r option to sort the last 7 state names coming from file state.txt in the reverse order.

tail -n 7 state.txt

tail command

tail command

Using Tail command with pipe `|`

tail -n 7 state.txt | sort -r

Using Tail command with pipe `|`

Using Tail command with pipe `|`

It can also be piped with one or more filters for additional processing. Like in the following example, we are using cat, head and tail command and whose output is stored in the file name list.txt using directive(>).

cat state.txt | head -n 20 | tail -n 5  > list.txt
cat list.txt

using `>` operator in tail command
using `>` operator in tail command

What is happening in this command let’s try to explore it. First cat command gives all the data present in the file state.txt and after that pipe transfers all the output coming from cat command to the head command. Head command gives all the data from start(line number 1) to the line number 20 and pipe transfer all the output coming from head command to tail command. Now, tail command gives last 5 lines of the data and the output goes to the file name list.txt via directive operator. 2.

?list=PLqM7alHXFySFc4KtwEZTANgmyJm3NqS_L

Conclusion

In this article we discussed the tail command in Linux serves as a complementary tool to the head command, specializing in displaying the last N lines of a given input. The default behavior prints the last 10 lines of specified files, with the option to include multiple file names, each preceded by its file name. The command’s syntax is succinct, using “tail [OPTION]… [FILE]…” for flexibility. Options such as -n, -c, -q, and -f provide users with precise control over the output, allowing customization based on line numbers, byte counts, file concatenation, and real-time log monitoring. The -v option ensures that data is consistently preceded by its file name. Additionally, the –version option provides details about the current version of the tail command. The command’s applications are diverse, ranging from straightforward data retrieval to complex piped operations with other Unix commands. In essence, the tail command proves to be an indispensable tool for efficiently navigating and monitoring files in the Linux environment.



Last Updated : 13 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads