I am writing a script which needs to check if any of the files in a given directory have been modified recently. I thought I could simply use find
, however it reports success even if it doesn't find a match:
$ touch afile
$ find . -mtime -1 -iname ????*
./afile
$ echo $?
0
$ find . -mtime +1 -iname ????*
$ echo $?
0
(I'm just using the iname predicate to exclude '.' here - but same behaviour with -type f
)
I would prefer not to have to parse the output of ls
as this can be temperamental.
Using test -n "$(find . -mtime -1 -iname ????*)"
seems to give the desired result but doesn't strike me as a particularly elegant solution.
I don't need to know which files have been modified - only if ANY have changed recently (where "recently" will usually be 1 day).
Is there a better way to do this?
(searching Google, I just get lots of SEO crap on how to execute a simple find)
find
implementation? Should we assume GNU tools? – terdon Jan 04 '24 at 12:52-print -quit
at the end, i.e. quitting after the first match (and priting the file for testing output vs. no output). – FelixJN Jan 04 '24 at 12:57find
, otherwise the shell may interpret them!find . -name '*.jpg'
not-name *.jpg
. – Kaz Jan 05 '24 at 01:07