Calling find more than once on the same folder tree

154 Views Asked by At

I'm running a find command multiple times on the same group of files. The results of my find commands are usually disjoint sets, AKA I'm running find -mmin +35; find -mmin -25, and doing different things to the results.

It seems sort of silly to search through the entire file system multiple times when one search would do. Is there a way to put multiple "search parameters" into a single find command and pipe the ouput to a different place depending on which parameter it satisfies?

Thanks

2

There are 2 best solutions below

2
On BEST ANSWER

Try this:

find . -mmin +35 -or -mmin -25

find supports several logical operators (-and, -or, -not). See the OPERATORS section of the man pages for more details.

==================== EDIT: In response to the question about processing the two matches differently, I do not know of a way to do this directly with find. Personally what I would do is to process the output in a for loop like this (note this is pesudo-code):

for f in `find . [condition 1] -or [condition 2]` ;do
    if [ f meets condition1 ]; then
        doSomething
    elif [ f meets condition2 ]; then
        doSomethingElse
    fi
done

I'm sure there are many bash gurus out there who could do this cleaner and more efficiently.

0
On

You can use tee to copy the output to two different subshells and process it differently.

find . -mmin +35 -or -mmin -25 | tee \
    >(while read f; do if [ $f meets condition1 ]; then; doSomething; done) \
    >(while read f; do if [ $f meets condition2 ]; then; doSomethingElse; done) \
    >/dev/null

That is the first solution that came to my mind after reading EJK's answer.

Edit: This is untested. A working example showing the principle is the following:

echo -e "abc\ndef\nauv\nafg\ndtt" | tee >(grep -P '^a' > a.txt) >(grep -P '^d' > d.txt) >/dev/null