[[!tag unix tip]]

I've been using the watch command a lot, lately. This is related to most command line programs in Unix not having progress bars.

For example, just now I'm waiting for a backup run in my crontab to finish, so that I can do some horrible mutilation to the machine being backed up. I keep an eye on the backup using this command:

watch pstree

watch runs the command every two seconds, by default, and handily crops excess output so the display doesn't scroll.

Sometimes I want to watch a more complicated command. For example, the output of "wc -l *.txt". If the set of text files changes while watch runs, then the following will not work:

watch wc -l *.txt

It won't work because the shell expands *.txt before watch is started. A workaround is to have watch invoke the shell on the raw command:

watch sh -c "'wc -l *.txt'"

The double quoting is needed so that the shell gets the command as one string (there's multiple levels of shell command line parsing going on).

watch is currently one of my favorite commands. It's almost as much fun as pv.

Update: bma mentions that my command can be simplified:

watch 'ls -l *.txt'

I stand corrected. This is much simpler than what I've been using. I'm now even more happy with watch.

Edit: Old discussion page.