Question : I am newbie in Linux.I have a question,How to read logs which are writing continuously.I am using the Linux CentOS Server and wants to read the mail.log which is filling continuously.I just want to read the logs at the same time,when log file writing is running
Answer: To read the file which are writing continuously and in running condition. We can use the command called tail
.
With the tail
command we will use some options.
What is Tail command
tail
command is used for getting output of the last part of files. By-default it shows last 10 number of lines from the file.As it name says, it show tail part of the file(means end part)
-f = output appended data as the file grows
-n = no. of lines . Bydefault with tail command,the last 10 lines are shown.By using -n option we can also specify ,last no. of lines
To get the output from last lines of file ,as well as wants to read file as it grows use the below given command
tail -f /path/of/file
Example
tail -f /var/log/mail.log
As we said earlier also,the above command will show output from growing file.When you first hit the command,on screen first it shows 10 last lines.And after this it shows output from growing file and number of line get increase automatically.
To stop tail -f .In other words, to stop getting output from growing file.Use the combination of keys CTRL+C from the keyboard . In MacOS ,it is COMMAND+C
How to use -n option with tail -f
By using -n option, you can specify how many number of last lines we want to see after hitting tail -f command.On screen it shows first time that number of lines.
Syntax:
tail -n number-in-digit -f /path-/of/file
Example In this example,first it shows 30 last lines.Because we have used -f option,then it shows appending lines from growing file.
You can replace 30 with any number as per your requirement.
tail -n 30 -f /var/log/mail.log