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 Ibmi Media, 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.
It is given below:
$ tail [option]... [file]...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
elephant1. Without any option it will print out the last 10 lines:
$ tail animal.txtThe output will be:
pig
bird
dragon
buffalo
cow
tiger
lion
bear
snake
elephant2. -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.txtThe output will be:
bear
snake
elephantYou can also use the below command:
$ tail -3 animal.txtThis 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.txtThe output will be:
bird
dragon
buffalo
cow
tiger
lion
bear
snake
elephant3. -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.txtThe Output will be:
andFor example, We want to print out from the 4th byte:
$ tail -c +4 animal.txtThe output will be:
cat
pig
bird
dragon
buffalo
cow
tiger
lion
bear
snake
elephant4. -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.txtWhere , animal.txt:
pig
bird
dragon
buffalo
cow
tiger
lion
bear
snake
elephantand, vege.txt:
tomato
broccoli
fish
yam
cauliflower
eggplant
cabbage
spinach
water spinach5. -v: Filenames always show the beginning
$ tail -v animal.txtOutput:
pig
bird
dragon
buffalo
cow
tiger
lion
bear
snake
elephantTo Check its version, execute:
$ tail --versionThis 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.