5 ms·
The above is perfectly fine for small directories, but in general the preferred way to loop over files is with find: find . ! -name . -prune
by ykonstant 2y ago
The above is perfectly fine for small directories, but in general the preferred way to loop over files is with find:
find . ! -name . -prune \
-exec grep -qE 'wav|draft' {} \; \
-exec "${action}" \; ;
Edit: I missed the herestring in the original code, so the above is wrong as mentioned in the comments; if your find has regex, you can use it to save one grep:
find . ! -name . -prune \
-regex '.*wav.*\|.*draft.*' \
-exec "${action}" \; ;
Otherwise you can call sh to printf the filename into a grep.
However, the point of my post is that find can perform seek, filter and execute, and should be used for all three unless it is really impossible (which is unlikely).