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.
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).
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 file_path file_event file_name; do
echo ${file_path}${file_name} event: ${file_event}
done
output:
Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
/var/log/messages event: MODIFY
/var/log/kern event: MODIFY
...
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.
I'm editing a LaTeX file and wanted to monitor it also for changes somewhere in the middle. I whipped up the following little shell script that proved useful to me. I hope it'll also come in handy to someone else.
#!/bin/bash
FILE="$1"
CMD="$2"
LAST=`ls -l "$FILE"`
while true; do
sleep 1
NEW=`ls -l "$FILE"`
if [ "$NEW" != "$LAST" ]; then
"$CMD" "$FILE"
LAST="$NEW"
fi
done
Save it as watch.sh and do chmod u+x watch.sh. Then I execute it as follows:
./watch.sh file.tex pdflatex
If you want the command only to be run if actual modification takes place, you can use `md5sum "$FILE"` instead of `ls -l "$FILE"`.
You can also use inotifywatch/inotifywait which hook into the kernels inotify subsystem. This way you can also watch for things like "open", "close" or "access".
But if you're simply want to get appended lines to stdout i agree on tail.
Tail is the standard, traditional, available everywhere unix tool. A little more sophisticated tool is multitail which can monitor several files simultaneously and does syntax highlighting.
If I want to be able to search around the file in addition to just tailing it, I use less with the "F" command.
When using tail, keep in mind that additional arguments are needed if the file might be rolling over or replaced by edit (default mode for vim's :w).
tail -f <filename> will cause tail to store the file descriptor and follow it. If the file is replaced the descriptor will be changed. The benefit of following the file descriptor is that if the file is renamed, you will still be following it.
tail --follow=<filename> will make tail track the named file by reopening it periodically to see if it has been replaced.
--retry is another useful option if you want to tail a log file but the file hasn't been created yet.
tail -F <filename> is a shortcut for --follow=<filename> --retry.
Do you mean
?
(Man page for tail)
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:
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).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:output:
I prefer using
less +FG
1 overtail -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:For really long logs, I find it convenient to use the
-n
option which turns off line numbering. From the manpage:1. Hat-tip to rgmarcha for pointing this out in the comments.
tail
is great ...less
can also be used start less on the file i.e.less myfile
then press Shift+F. This hasless
act astail
.I'm editing a LaTeX file and wanted to monitor it also for changes somewhere in the middle. I whipped up the following little shell script that proved useful to me. I hope it'll also come in handy to someone else.
Save it as
watch.sh
and dochmod u+x watch.sh
. Then I execute it as follows:./watch.sh file.tex pdflatex
If you want the command only to be run if actual modification takes place, you can use
`md5sum "$FILE"`
instead of`ls -l "$FILE"`
.you can use the tailf command its very easiest one
You can also use inotifywatch/inotifywait which hook into the kernels inotify subsystem. This way you can also watch for things like "open", "close" or "access".
But if you're simply want to get appended lines to stdout i agree on tail.
Tail is the standard, traditional, available everywhere unix tool. A little more sophisticated tool is multitail which can monitor several files simultaneously and does syntax highlighting.
If I want to be able to search around the file in addition to just tailing it, I use less with the "F" command.
When using tail, keep in mind that additional arguments are needed if the file might be rolling over or replaced by edit (default mode for vim's :w).
tail -f <filename>
will cause tail to store the file descriptor and follow it. If the file is replaced the descriptor will be changed. The benefit of following the file descriptor is that if the file is renamed, you will still be following it.tail --follow=<filename>
will make tail track the named file by reopening it periodically to see if it has been replaced.--retry
is another useful option if you want to tail a log file but the file hasn't been created yet.tail -F <filename>
is a shortcut for--follow=<filename> --retry
.