It is the complement of the main command. The tail command, as the name implies, prints the last data number N of the given entry. By default, prints the last 10 lines of the specified files. If more than one file name is provided, the data in each file is preceded by its file name.
tail [OPTION]… [ARCHIVE]…
Consider two files that have name state.txt and capital.txt contains all the names of the Indian states and capitals respectively.
$ cat status.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
With no option, it displays only the last 10 lines of the specified file. Example
: $ tail state.txt Odisha Punjab Rajasthan Sikkim Tamil Nadu Telangana Tripura Uttar Pradesh Uttarakhand West Bengal
1. -n num: Print the last ‘num’ lines instead of the last 10 lines. It is mandatory to specify num in the command, otherwise it displays an error. This command can also be written as without symbolizing the character ‘n’, but the ‘-‘ sign is required.
$ tail -n 3 status.txt Uttar Pradesh Uttarakhand West Bengal OR $ tail -3 state.txt Uttar Pradesh Uttarakhand West Bengal
Tail command also comes with a ‘+’ option that is not present in the head command. With this option, the tail command prints the data from the specified line number of the file instead of end. For the command: queue +n file_name, the data will start printing from line number ‘n’ to 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 of the specified file. The new line counts as a single character, so if tail prints a new line, it will count it as a byte. In this option it is mandatory to write -c followed by positive or negative num depending on the requirement. By +num, displays all data after omitting num bytes from the start of the specified file, and by –num, displays the last num bytes of the specified file. note: With no positive or negative sign before num, the command displays the last num bytes of the specified file.
With negative num $ tail -c -6 state.txt Bengal O$ tail -c 6 state.txt Bengal With positive num $ tail -c +263 state.txt Nadu Telangana Tripura Uttar Pradesh Uttarakhand
3. -P: Used if more than 1 file is provided. Because of this command, the data in each file is not preceded by its file name.
Unused -q option $ tail state.txt capital.txt state.txt Odisha Punjab Rajasthan Sikkim Tamil Nadu Telangana Tripura Uttar Pradesh Uttarakhand Capital of West Bengal.txt Dispur Patna Raipur Panaji Gandhinagar Chandigarh Shimla Srinagar Ranchi With the use of -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 Bangalore
4. -f: This option is mainly used by system administration to monitor the growth of log files written by many Unix programs as they run. This option displays the last ten lines of a file and will be updated when new lines are added. As new lines are written to the log, the console will update with the new lines. The message does not return even after the job is finished, so we have to use the kill key to abort this command. In general, applications write error messages to log files. You can use the -f option to check whether error messages appear in the log file.
$ tail -f logfile
5. -v: When you use this option, the data in the specified file is always preceded by its file name.
$ tail -v state.txt ==> state.txt <== Odisha Punjab Rajasthan Sikkim Tamil Nadu Telangana Tripura Uttar Pradesh Uttarakhand West Bengal
6. -version: This option is used to display the version of tail that is 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. GPLv3+ license: GNU GPL version 3 or later. This is free software: you are free to change and redistribute it. NO WARRANTY, to the extent permitted by law. Writing by Paul Rubin, David MacKenzie, Ian Lance Taylor and Jim Meyering.
Applications in the Command 1 queue
. How to use glue with tubes (|): The tail command can be piped with many other unix commands. In the following example, the output of the tail command is given as input to the sort command with the -r option to sort the last 7 state names from the file state.txt in reverse order.
$ tail -n 7 status.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 piped with one or more filters for further processing. As in the following example, we are using the cat, head and tail command and whose output is stored in the list of file names.txt using directive(>).
$ cat status.txt | head -n 20 | tail -n 5 > list.txt $ cat list.txt Manipur Meghalaya Mizoram Nagaland Odisha
What is happening in this command we will try to explore. The first cat command provides all the data present in the file state.txt and after that the pipeline transfers all the output from the cat command to the head command. The head command provides all data from the start (line number 1) to line number 20 and the pipeline transfers all output from the head command to the tail command. Now, the tail command gives the last 5 lines of the data and the output goes to the list of file names.txt through the policy operator.2. Print line between M and N lines This article is contributed by Akash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See their article listed on the GeeksforGeeks homepage and help other Geeks.
Please write comments if you find something wrong, or if you want to share more information about the topic discussed above.