Linux and Unix tail command is one of the important command.
Command tail displays the bottom/end of the file at the terminal.
When “tail” command is used without any option it will display the bottom/end ten lines of the file.
Syntax of tail Command in Unix and Linux
Syntax:
1 | $ tail filename [press enter] |
Example of tail Command in Unix and Linux
Suppose we have a file Employee.txt as shown below and we want to display the bottom/end of this file.
By default tail command will display the bottom/end ten lines of the file.
1 | $ tail Employee.txt [press enter] |
Output
1 2 3 4 5 6 7 8 9 10 | Employee Details Name | Post | Salary | DOB | Age | Aditya | DGM | 100000 | 03-02-1987 | 30 | Kumar | GM | 700000 | 05-02-1977 | 41 | Manish | CA | 500000 | 10-02-1997 | 20 | Rajeev | CLERK | 300000 | 05-05-1987 | 31 | Ashok | DGM | 800000 | 03-12-1977 | 40 | Satwant | GM | 500000 | 05-02-1997 | 21 | Jack | CA | 600000 | 10-12-1990 | 28 | Bharat | CLERK | 400000 | 04-03-1987 | 31 | |
By default tail displayed bottom ten lies of file.
Options of tail command
tail -n option in Unix and Linux
Option (-n): potion “–n” is used to specify the number of lines to be display from bottom. Suppose we want to display the bottom 4 lines of file Employee.txt then the command is as follows:
1 | $ tail -n 4 Employee.txt [press enter] |
Or
1 | $ tail 4 Employee.txt [press enter] (On some system) |
Output
1 2 3 4 | Ashok | DGM | 800000 | 03-12-1977 | 40 | Satwant | GM | 500000 | 05-02-1997 | 21 | Jack | CA | 600000 | 10-12-1990 | 28 | Bharat | CLERK | 400000 | 04-03-1987 | 31 | |
Option (+ count): potion “+” is used to specify the line number from where the file to be displayed. Suppose we want to display line number 4 to onward of the file Employee.txt then the command is as follows:
1 | $ tail + 4 Employee.txt [press enter] |
Above command will display line number 4 to end of the file of file Employee.txt.
tail -f command option use and example in Unix and Linux
tail -f to show /var/log/auth.log updates
1 | $ tail -f /var/log/auth.log [press enter] |
It will show real time file updated if any new entry added then it shows the updated data.
tail -f to show tomcat/logs/catalina.out updates
1 | $ tail -f /opt/tomcat/logs/catalina.out [press enter] |
used to show real time updates in catalina.out
Show n lines with tail -f command
tail -f can be used with -n to show number of lines
tail -n0 -f
Shows 0 lines with -f option
tail -n5 -f
shows 5 lines with -f option
Tail -f output with line number
tail -f | nl
display the file content with line number.
Searching Pattern Using tail -f and grep
we can use grep with tail -f to search specific pattern
1 | $ tail -f /opt/tomcat/logs/catalina.out | grep -n http [press enter] |
Reference
Read More
Linux and Unix head command tutorial with examples
Linux and Unix sort command tutorial with examples