tail Command in Linux - With examples

tail command is the supplement of the head command. This command tells you the last data of the file input. Normally by default, the tail command prints out the last 10 lines of the file.

tail command is the best and useful way to see the most recently added data. It can also monitor a file and show each new data added to that file as they happen.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Linux command queries.

In this context, we shall look into how to use the tail command in Linux.


What is the syntax of tail command ?

It is given below:

$ tail [option]... [file]...


tail command Options with examples

Here we will use an file with name "animal.txt" with the following contents:

$ cat animal.txt
dog
cat
pig
bird
dragon
buffalo
cow
tiger
lion
bear
snake
elephant


1. Without any option it will print out the last 10 lines:

$ tail animal.txt

The output will be:

pig
bird
dragon
buffalo
cow
tiger
lion
bear
snake
elephant


2. -n num: Specifies the number of last lines to be printed

For example, We will print out the last 3 lines:

$ tail -n 3 animal.txt

The output will be:

bear
snake
elephant

You can also use the below command:

$ tail -3 animal.txt

This will yield the same result.

With + option, it will print out from the specified line to the last line

For example, We will print out from the 4th line to the last line:

$ tail +4 animal.txt

The output will be:

bird
dragon
buffalo
cow
tiger
lion
bear
snake
elephant


3. -c num: Prints out the last characters of the specified file.

Each character is treated as 1 byte. With -num, it will print out the last num characters of the file. With +num, it will skip the first num characters and start printing out from the num character.

For example, We will print out the last 3 characters:

$ tail -c -4 animal.txt

The Output will be:

and

For example, We want to print out from the 4th byte:

$ tail -c +4 animal.txt

The output will be:

cat
pig
bird
dragon
buffalo
cow
tiger
lion
bear
snake
elephant


4. -q: To execute multiple files at once

We will use the tail command with 2 files animal.txt and vege.txt:

$ tail animal.txt vege.txt

Where , animal.txt:

pig
bird
dragon
buffalo
cow
tiger
lion
bear
snake
elephant

and, vege.txt:

tomato
broccoli
fish
yam
cauliflower
eggplant
cabbage
spinach
water spinach


5. -v: Filenames always show the beginning

$ tail -v animal.txt

Output:

pig
bird
dragon
buffalo
cow
tiger
lion
bear
snake
elephant


How to check the version of tail utility ?

To Check its version, execute:

$ tail --version


[Need assistance in fixing Linux packages issues ? We can help you. ]

This article covers how to use the tail command in Linux. In fact, The Linux tail command displays data from the end of a file. It can even display updates that are added to a file in real-time. It can also monitor a file and display each new text entry to that file as they occur. This makes it a great tool to monitor log files.

Related Posts