Thursday, August 15, 2013

grep less tail

This is my first post for the "Shell tips for developer" idea. The idea is to work with some of my friends to share useful shell commands and simple shell script tips we use on a day to day basis.

Hopefully, these tips will be of some help to our fellow developers and make their daily coding/debugging life just a little bit more productive.


Today, I am going to share with you guys how we can use tail,less and grep to better monitoring log files and get to the useful information while monitoring them.

tail

We start with the basic tail command.

man tail

tell you the following

DESCRIPTION
    The tail utility displays the contents of file or, by default, its standard input, to the standard out-put.`

If you want to monitor a log file, you use

tail -f <file>

This will continuously display the content of the log file as it grows. This has to be one of the most used command when you debug a running application.

tail -f | grep

tail -f is very useful. However, some times, the amount of content in the log file can be quite disconcerting. You may only be interested in the log file content when a particular information appears. For example if you are debugging some application that sends emails, and whenever an email is sent, the application will print in the log file something like

sending email with content : content ... blah blah

Now you only want to see the new content in the log file when an email is sent. In this case you can use tail with the grep command. e.g.

tail -f <file> | grep "sending email with content" 

what happens here is that the tail command will output the content of the log file and pipe it to the grep command. Then grep will do what grep does best and only displays the content from tail when "sending email with content" is found. Even better, it will also display the next couple of lines after the "sending email with content" so you have a better idea of what is going on there.

less than tail

All is good, but now you want to go a bit interactive, what you want to do now is to see the content of the log file as it becomes available, but also be able to search in the log file, highlight some of the keywords and navigate around in the log file without quitting the file.

You can use the less command for all this.

  1. less as tail:(not sure why you want to do this but ) you can use less +F <file> to make less acting just like tail -f.
  2. less +F (you can also use less then use F to get in the "updating" mode.
  3. use ctrl + c to exit the "updating" mode and back to the "normal" mode, where you can start navigate around the file.
  4. you can use all less commands once you are back in the "normal" mode, as expected.

one drawback of using less instead of tail is that less command will load the log file in its entirety (eventually), obviously for large log files, this may cause some issues.