[GLLUG] find / shell weirdness

Nix nix at esperi.org.uk
Sat Oct 12 23:36:38 UTC 2013


On 8 Oct 2013, tid said:

> Folks,
>
> I have a small bash script which watches a number of folders for files
> added in the last 10 minutes:
>
> ($mydirs below is just a list of ftp folders which clients upload to )
>
> ===================================================
> find $mydirs -type f -a -cmin -10 -print > /tmp/filewatcher.count
>
> if [ `wc -l /tmp/filewatcher.count | awk ' { print $1 } ' ` -gt 0 ]
> then
>         # email me notification bla bla bla
>         [ snip ]
> fi
> ====================================================

Safer (and faster) proposal:

if [ $(find "$mydirs" -type f -a -cmin -10 -a -quit 2>/dev/null | wc -c) -gt 0 ]; then
    ... something found ...
fi

This no longer causes trouble if $mydirs contains spaces, no longer
writes a pointless temporary file, and no longer wastes time iterating
over all the files if you only care if at least one is present, and no
longer spams stderr on permission errors.

(Of course, if you need the names... it's still better done as a
pipeline, but you probably don't want the if, the -quit, or the wc, and
probably want a -print0 and an xargs -0r instead.)

-- 
NULL && (void)




More information about the GLLUG mailing list