Bash – How to have find recurse into subdirectories when using -prune option

bashcentosfind

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.

Best Answer

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 ).

Related Question