Shell command to monitor changes in a file

I know there was a command on Unix that I could use to monitor a file and see changes that are getting written to it. This was quite useful especially for checking log files.

Do you know what it is called?


Solution 1:

Do you mean

tail -f logfile.log

?

(Man page for tail)

Solution 2:

You probably meant tail, as per Jon Skeet's answer.

Another useful one is watch; it allows you to run a command periodically and see the output full screen. For example:

watch -n 10 -d ls -l /var/adm/messages

Will run the command ls -l /var/adm/messages every 10 seconds, and highlight the difference in the output between subsequent runs. (Useful for watching how quickly a logfile is growing, for example).

Solution 3:

inotifywait from inotify-tools is useful if you want to run a command every time a file (or any files in a directory) change. For example:

inotifywait -r -m -e modify /var/log | 
   while read path _ file; do 
       echo $path$file modified
   done

Solution 4:

I prefer using less +FG1 over tail -f because I find myself needing to search a log file for a specific error or ID. If I need to search for something, I type ^C to stop following the file and ? to start searching backwards.

Key bindings are pretty much the same as in vi. Any command can be initialized on startup using the + option:

+cmd   Causes  the  specified  cmd  to be executed each time a new file is
       examined.  For example, +G causes less to  initially  display  each
       file starting at the end rather than the beginning.

For really long logs, I find it convenient to use the -n option which turns off line numbering. From the manpage:

-n or --line-numbers
          Suppresses line numbers.  The default (to use line numbers)  may
          cause  less  to run more slowly in some cases, especially with a
          very large input file.  Suppressing line  numbers  with  the  -n
          option  will  avoid this problem.  Using line numbers means: the
          line number will be displayed in the verbose prompt and in the =
          command,  and the v command will pass the current line number to
          the editor (see also  the  discussion  of  LESSEDIT  in  PROMPTS
          below).

1. Hat-tip to rgmarcha for pointing this out in the comments.

Solution 5:

Tail is great ... less can also be used start less on the file i.e. less myfile then press Shift+F. This has less act as tail.