Tail Command in Linux is used to display the last part of a file, showing recent content such as logs or updates.
By default, it shows the last 10 lines of a file.
Commonly used for monitoring log files and debugging.
You can customize the number of lines displayed using the -n option.
Useful for viewing the most recent entries without opening the entire file.
Example
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.
tail state.txt
Here we will only get names of last 10 states after using tail command.
tail command in Linux
Syntax of Tail Command in Linux
tail [OPTION]... [FILE]...
Options and Practical Examples of Tail Command in Linux
This section covers the most commonly used options and practical examples of the tail command to help you efficiently view and monitor the end of files in Linux.
1. `-n` num Option in Tail Command in Linux
By default, the tail command displays the last 10 lines of a file.
To print a specific number of lines, use the -n option followed by the desired number.
The num value is mandatory; leaving out a number will cause an error.
You can also use the command without the n option, but the - sign is required.
tail -n 3 state.txt
or
tail -3 state.txt
The tail command also supports the ‘+’ option, which is not available in the head command.
Using this option, the command prints data starting from the specified line number to the end of the file.
tail +25 state.txt
tail +n option in Linux2. `-c` num Option in Tail Command in Linux
The -c option in the tail command is used to print the last ‘num’ bytes from the specified file.
Newlines count as single bytes, so each newline character is also included in the count.
It is mandatory to specify -c followed by a positive or negative number depending on your requirement.
Using -c -num displays the last num bytes from the file.
Using -c +num displays all data after skipping num bytes from the beginning of the file.
Note: Without positive or negative sign before num, command will display the last num bytes from the file specified.
Example
Display the last 7 bytes of the file:
tail -c -7 state.txt
or
tail -c 7 state.txt
-c option in tail command in Linux (using negative)
Display all data after skipping the first 263 bytes:
tail -c +263 state.txt
-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.
With using -q option in tail command in Linux4. `-f` Option in Tail Command in Linux
The -f option in the tail command is used to continuously monitor a file in real-time.
It displays the last 10 lines of a file and automatically updates as new lines are added.
Commonly used by system administrators to track log files or error messages as programs run.
The command does not exit automatically — you must stop it manually using Ctrl + C.
$ 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 Linux6. `--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
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
Using Tail command with pipe `|`
tail -n 7 state.txt | sort -r
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(>).