Tail command in Linux with examples (original) (raw)

Last Updated : 9 Jan, 2026

Tail Command in Linux is used to display the last part of a file, showing recent content such as logs or updates.

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.

**Command:

cat state.txt

**Output:

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

tail state.txt

tail command in Linux

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

tail -n 3 state.txt
or
tail -3 state.txt

85

tail +25 state.txt

86

tail +n option in Linux

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

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

Example

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)

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

**Command:

cat capital.txt

**Output:

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

**Command:

tail state.txt capital.txt

**Output:

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

****$ 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

**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