Tail is a complement to the head command. As its name implies, the tail command outputs the last N data numbers of the given input. By default, the tail command prints the last 10 lines of the specified file. If more than one file name is provided then the file name will be preceded by the data from each file.
tail [OPTION]... [FILE]...
Consider two files with names state.txt and capital.txt, contains all names of the respective states and capitals of India.
$ 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 options, only the last 10 lines of the specified file are displayed.
$ tail state.txt Odisha Punjab Rajasthan Sikkim Tamil Nadu Telangana Tripura Uttar Pradesh Uttarakhand West Bengal
1. -n num
Prints the last ‘num’ lines instead of the last 10. “num” is required to be specified in the command, otherwise it will display an error. This command can also be written without the character ‘n’ but the ‘-‘ is required.
$ tail -n 3 state.txt Uttar Pradesh Uttarakhand West Bengal OR $ tail -3 state.txt Uttar Pradesh Uttarakhand West Bengal
The tail command also comes with a ‘+’ option not included in the head command. With this option, the tail command will print data starting from the specified number of lines of the file instead of the last lines in the file. For the command: tail + n name_file, data will start printing from line number ‘n’ until the end of the specified file.
$ tail +25 state.txt Telangana Tripura Uttar Pradesh Uttarakhand West Bengal
2. -c num
Prints the last ‘num’ bytes from the specified file. The new line counts as a single character, so if the tail prints a new line, it counts as one byte. In this option, required to write -c followed by a positive or negative num depending on the requirement. With + num, it displays all data after omitting num bytes from the start of the specified file and with -num, it shows the last num byte from the specified file.
Note: Without a positive or negative sign before num, the command will display the last num byte from the specified file.
With negative num $ tail -c -6 state.txt Bengal OR $ tail -c 6 state.txt Bengal With positive num $ tail -c +263 state.txt Nadu Telangana Tripura Uttar Pradesh Uttarakhand
It is used if there is more than 1 file. Because of this command, the filename is no longer preceded by data from each file.
Without using -q option $ tail state.txt capital.txt state.txt Odisha Punjab Rajasthan Sikkim Tamil Nadu Telangana Tripura Uttar Pradesh Uttarakhand West Bengal capital.txt Dispur Patna Raipur Panaji Gandhinagar Chandigarh Shimla Srinagar Ranchi With using -q option $ tail -q state.txt capital.txt Odisha Punjab Rajasthan Sikkim Tamil Nadu Telangana Tripura Uttar Pradesh Uttarakhand West BengalDispur Patna Raipur Panaji Gandhinagar Chandigarh Shimla Srinagar Ranchi Bengaluru
This option is mainly used by system administrators to monitor the development of log files written by multiple Unix programs while they are running. This option displays the last 10 lines of the file and will update as new lines are added.
As new rows are recorded in the log, the console will update the new rows. The prompt does not return even after the job ends, so the interrupt key must be used to cancel the command. Generally, applications log error messages to log files. You can use options -f to check for error messages as they appear in the log file.
$ tail -f logfile
Using this option, the filename is always preceded by the data from the specified file.
$ tail -v state.txt ==> state.txt <== Odisha Punjab Rajasthan Sikkim Tamil Nadu Telangana Tripura Uttar Pradesh Uttarakhand West Bengal
This option is used to display the tail version currently running on your system.
$ tail --version tail (GNU coreutils) 8.26 Packaged by Cygwin (8.26-1) Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Paul Rubin, David MacKenzie, Ian Lance Taylor, and Jim Meyering.
Tail command applications
1. How to use tail with pipe (|)
The tail command can be combined with many other Unix commands. In the following example, the output of the command tail is given as input to the command sort with option -r to sort the last 7 states from the file state.txt in reverse order.
$ tail -n 7 state.txt Sikkim Tamil Nadu Telangana Tripura Uttar Pradesh Uttarakhand West Bengal $ tail -n 7 state.txt | sort -r West Bengal Uttarakhand Uttar Pradesh Tripura Telangana Tamil Nadu Sikkim
It can also be hooked up to one or more filters for additional processing. Like in the following example, we are using the cat, head, and tail commands and its output is stored in the filename. list.txt equal >.
$ cat state.txt | head -n 20 | tail -n 5 > list.txt $ cat list.txt Manipur Meghalaya Mizoram Nagaland Odisha
What’s going on in this command, try to discover it. Comeinand cat The first provides all the data contained in the file state.txt and then, the pipe passes all the output from the command cat to order head. Comeinand head provides all of the data from start (line 1) to line 20 and passes all output from the command head to order tail. Now, the command tail Give the last 5 lines of data and the output passed to the filename list.txt through the directive operator.
2. Print the lines between lines M and N
For this purpose, you will have to use the head, tail, and pipe (|) commands. The command is: head -M file_name | tail – (M N), because the first line is M and the tail command cuts lines between M and N, starting at the end. Assuming that from state.txt file, you have to print lines 10 through 20.
$ head -n 20 state.txt | tail -10 Jharkhand Karnataka Kerala Madhya Pradesh Maharashtra Manipur Meghalaya Mizoram Nagaland Odisha
Source link: Tail command in Linux