Chaining commands within `watch`
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 videos on Youtube
Comments
-
Fake Name almost 2 years
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'
returnswatch: 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!).-
Admin over 11 yearsHave you tried
watch -x 'find . | wc -l'
? -
Admin over 11 years@ire_and_curses -
watch -x 'ls'
watch: -x: No such file or directory
-
Admin over 11 yearsThe version of
watch
is some ancient busybox version. It ONLY supports the-n
flag. -
Admin over 11 yearsOk, so what happens if you place your
find . | wc -l
command in a shellscript, and execute that instead (watch myscript.sh
)? -
Admin over 11 years@ire_and_curses - I'm pretty sure that would work, I was hoping to avoid making up lots and lots of shellscript files.
-
-
xrfang over 11 yearsThe 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? -
Stéphane Chazelas over 11 yearsOops, sorry. I got confused here. I'll update the answer.
-
Fake Name over 11 yearsThis works. All I can say is "FFFfffffuuuu busybox!"
-
Stéphane Chazelas over 11 yearsWell, 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)