Monday, 21 December 2015

How To Use Tail Command In Unix/Linux

In this article we will see how tail command works and its real time practical example.

File1:abc.txt
10001|A1|TRANS|Y|10000
10002|A2|MED|Y|20000
10003|A3|FIN|N|10000
10004|A4|HR|Y|20000
10005|A5|CSR|N|50000
10006|A6|TRANS|Y|30000
10007|A7|FIN|N|40000

File2: xyz.txt
20001|B1|TRANS|Y|10000
20002|B2|MED|Y|20000
20003|B3|FIN|N|10000
20004|B4|HR|Y|20000
20005|B5|CSR|N|50000

How to read last 3 line of a file
$ tail -n 3 abc.txt

[OR]

$ tail -3 abc.txt

Output: Will display the last 3 lines of the file abc.txt
10005|A5|CSR|N|50000
10006|A6|TRANS|Y|30000
10007|A7|FIN|N|40000

How to print line from a specefied line (nth)
$ tail -n +3 abc.txt

Note: observe how we have used + sign with the number in tail, while in head we have used - sign.

Output: Will display the all the lines from 3rd line to the end of file
10003|A3|FIN|N|10000
10004|A4|HR|Y|20000
10005|A5|CSR|N|50000
10006|A6|TRANS|Y|30000
10007|A7|FIN|N|40000

Default tail will display last 10 lines of a file
$ tail abc.txt

Output: Has displayed complete file as it has less than 10 lines.
10001|A1|TRANS|Y|10000
10002|A2|MED|Y|20000
10003|A3|FIN|N|10000
10004|A4|HR|Y|20000
10005|A5|CSR|N|50000
10006|A6|TRANS|Y|30000
10007|A7|FIN|N|40000


How to see lines from multiple files
$ tail abc.txt xyz.txt

Output: Will display the last ten lines of both abc.txt and xyz.txt, with a header before each that indicates the filename.

How display the first 3 lines from multiple table
$ tail -n +3 abc.txt xyz.txt

Output:Will displya the line from 3rd line to the end of file.
==> abc.txt <==
10003|A3|FIN|N|10000
10004|A4|HR|Y|20000
10005|A5|CSR|N|50000
10006|A6|TRANS|Y|30000
10007|A7|FIN|N|40000

==> xyz.txt <==
20003|B3|FIN|N|10000
20004|B4|HR|Y|20000
20005|B5|CSR|N|50000

How to see the the lines from 4th line to the end for every .txt file in the directory
$ tail -n +4 *.txt

Output: Display the line from the 4th line from every .txt file in the working directory
==> abc.txt <==
10004|A4|HR|Y|20000
10005|A5|CSR|N|50000
10006|A6|TRANS|Y|30000
10007|A7|FIN|N|40000

==> xyz.txt <==
20004|B4|HR|Y|20000
20005|B5|CSR|N|50000

View continuously increasing file in real time using tail command
$ tail -f abc.txt

Output: Will display the latest added lines in the growing file abc.txt

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...