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!).

  • Have you tried watch -x 'find . | wc -l'? – ire_and_curses Nov 1 '12 at 5:28
  • @ire_and_curses - watch -x 'ls' watch: -x: No such file or directory – Fake Name Nov 1 '12 at 6:02
  • The version of watch is some ancient busybox version. It ONLY supports the -n flag. – Fake Name Nov 1 '12 at 6:03
  • 1
    Ok, so what happens if you place your find . | wc -l command in a shellscript, and execute that instead (watch myscript.sh)? – ire_and_curses Nov 1 '12 at 6:20
  • @ire_and_curses - I'm pretty sure that would work, I was hoping to avoid making up lots and lots of shellscript files. – Fake Name Nov 1 '12 at 7:05

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).

| improve this answer | |
  • The manpage for my version of watch says: Note that command is given to "sh -c" which means that you may need to use extra quoting to get the desired effect. Isn't this the opposite of what you've just said? – ire_and_curses Nov 1 '12 at 7:40
  • Oops, sorry. I got confused here. I'll update the answer. – Stéphane Chazelas Nov 1 '12 at 7:45
  • 1
    This works. All I can say is "FFFfffffuuuu busybox!" – Fake Name Nov 1 '12 at 8:06
  • 1
    Well, I find the busybox behavior makes more sense. There's no need to spawn a shell when you don't need to like in most cases (though not yours) – Stéphane Chazelas Nov 1 '12 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.