Shell – Chaining commands within `watch`

busyboxshellwatch

Ok, this is driving me a bit nuts.

I'm trying to watch the number of files in a set of subdirectories.

find . | wc -l correctly returns the number of sub-folders and files.

However, watch 'find . | wc -l' returns
watch: find . | wc -l: No such file or directory
in the watch screen.
It returns the same with double-quotes, or backquotes (`).


This is on an ancient version of bash:
GNU bash, version 3.00.16(2)-release (i486-slackware-linux-gnu)
Copyright (C) 2004 Free Software Foundation, Inc.

This is also an embedded device, so it's running busybox (BusyBox v1.1.0 (2010.06.14-02:47+0000) multi-call binary) rather then the normal gnu utils, so most of the switches and functionality of most of the common tools is not there either.

So that has to be taken into account. However, the linux install is baked onto a disk-on-module, so there is no easy way to update it.

The same command (watch 'find . | wc -l') works correctly on a more recent linux install, so this question is more about dealing with out-of-date bash then what is wrong with this exact snippet (since it seems to be correct elsewhere!).

Best Answer

Depending on your implementation/version of watch, it may not start a shell to interpret a command line, but instead runs a command that takes as argument the arguments it received itself. So, in that case, if you need it to run a shell command line, you need to start a shell explicitly as in:

watch sh -c 'find . | wc -l'

See also the inotifywait -rm . command (if on Linux) to monitor activity in a directory.

Also note that find . | wc -l only returns the number of files (excluding the .. entries) if the file names don't contain newline characters. If that may be a problem, you could do:

find .//. | grep -c //

Also note that there's no GNU or Unix utility by the name of watch. There's a watch command in the procps suite of tools for Linux, there's a watch implementation in busybox. On BSDs, watch does something completely different. watch is not a standard command (in none of POSIX, Unix or LSB specifications).

Related Question