Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I see that the find command does not descend into subdirectories when you're using the -prune option. How do I tell find to recurse into the sub directories, but also ignore some stuff?

Specifically, I want to search the /var/lib/foo directory, but exclude any directories with the name .snapshot/.

sudo find /var/lib/foo -prune -name '.snapshot'

With the above, it properly ignores .snapshot, but doesn't go into the sub dirs. FYI, I'm working in bash on a CentOS 6.3 host.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You need to do:

sudo find /var/lib/foo -name '.snapshot' -prune -o -print

it will print whatever is not named ".snapshot", and if ".snapshot" is a directory it will also not descend into it.

why ? because "-prune" is an action (as '-print' is also another action), doing nothing except preventing to go further down in the subdir. And it always return "true", so here, ( -name ... -prune ) is true if and only if the file or dir is named "...", and you you want everything else, hence the -o ( -print ).

share|improve this answer
    
I found this nice so answer by @Laurence Gonsalves which explains it so much better (including a nice warning about not adding the -print) –  Olivier Dulac Jan 16 '13 at 16:18
    
Works perfect, thanks Olivier. I tend to forget the ordering of things with the find command, such as actions. –  Banjer Jan 16 '13 at 18:01
    
parenthesis also helps : find /someplace \( some tests [and actions] \) -o \( some other tests [and actions] \) , with possibility of multiple layers of parenthesis. –  Olivier Dulac Jan 16 '13 at 18:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.