nanker
nanker

Reputation: 603

find -mindepth -prune conflict?

I have a dir structure like:

gameplay/ch1_of_182/ajax.googleapis.com/...
gameplay/ch1_of_182/platform.twitter.com/...
gameplay/ch1_of_182/privacy-policy.truste.com/...
gameplay/ch1_of_182/www.facebook.com/...
gameplay/ch1_of_182/www.gameplay.com/...
gameplay/ch1_of_182/www-mega.gameplay.com/...
gameplay/ch2_of_182/ajax.googleapis.com/...
gameplay/ch2_of_182/platform.twitter.com/...
gameplay/ch2_of_182/privacy-policy.truste.com/...
gameplay/ch2_of_182/www.facebook.com/...
gameplay/ch2_of_182/www.gameplay.com/...
gameplay/ch2_of_182/www-mega.gameplay.com/...
...
gameplay/ch182_of_182/ajax.googleapis.com/...
gameplay/ch182_of_182/platform.twitter.com/...
gameplay/ch182_of_182/privacy-policy.truste.com/...
gameplay/ch182_of_182/www.facebook.com/...
gameplay/ch182_of_182/www.gameplay.com/...
gameplay/ch182_of_182/www-mega.gameplay.com/...

created using wget. Now I want to delete all of the directories and all the files they contain except for the two directories with "gameplay.com" in their name.

I have been trying different variants of:

find . -mindepth 2 ! -path "*gameplay.com" -prune -exec rm -r {} \;

but without luck.

When run from the gameplay parent dir, the 4 of 6 directories in each of the 182 directories (actually only using 1 for testing purposes) that do not contain the "gameplay.com" name pattern are deleted along with all their contents, as desired. And although the 2 directories that do contain the "gameplay.com" name pattern are left undeleted, all the files contained within each are deleted, which is not good.

I thought the -prune option was supposed to let find know to basically ignore the directory specified, but I must be specifying it wrong, because that is not happening, leaving me with two empty directories in each of the 182 parent dirs.

I think there is a potential conflict with the -mindepth and -prune options, but because I am running the command from the gameplay parent directory, without it all 182 child directories, each containing two directories I want left in tact, are deleted.

I guess I could write a for loop to cycle thru each dir, but it seems to me find should be able to accomplish this task in one foul swoop if someone doesn't mind shedding some light how.

Upvotes: 1

Views: 217

Answers (1)

Reinstate Monica Please
Reinstate Monica Please

Reputation: 11593

Logically, this is find . -mindepth 2 \( ! -path "*gameplay.com" \) -a \( -prune \) -a \( -exec rm -r {} \; \)

So since your -prune comes after the path check, it's never gotten to if your path check is false. Try switching the order.

find . -mindepth 2 -prune ! -path "*gameplay.com" -exec rm -r {} \;

Upvotes: 2

Related Questions