File monitoring in a real time using “tail” and “grep”

2013-10-25 Linux

Sometimes (especially when you are an administrator) there may be a need, to see the content of a specific file in a real time – for example log file.

You could open file, go down to see last entries, close file and then repeat the whole process to see if there have been done some changes but for sure you will say, that this method is annoying and not so effective. I will show you how to see files content in a real time (without the need to reopen them, to see if changes occured). Two commands for today – tail and grep.

Open terminal and log into one of your web servers:

ssh -l root 10.1.0.186

Now let’s go to logs directory:

cd /var/log/apache2

We are curious about the ammount of request sent to our server, so we should browse file called access.log:

tail -f access.log

You should see now the real time preview of access.log. The speed of appearing new lines is connected with number of request your server is handling right now. To quit live preview press:

CTRL + C

Much more effective results we will get usign both “tail” command and “grep” (narrowed results). For example we want to see only request for JavaScript files:

tail -f access.log | grep ".js"

Isn’t that awesome? Please notice that “the magic” is done by the “-f” parameter after tail command – it means “follow”.