atkayla
atkayla

Reputation: 8849

Find and remove all directories with multiple names?

find ./myfolder -type d -name example -exec rm -rf {} +

Removes every directory called example.

But how would I get something like this to work?

find ./myfolder -type d -name example -o -name docs -exec rm -rf {} +

To remove all example AND docs directories?

Upvotes: 0

Views: 90

Answers (1)

that other guy
that other guy

Reputation: 123470

You have to add explicit grouping:

find ./myfolder -type d \( -name example -o -name docs \) -exec rm -rf {} +

This is required because find, like in programming and math in general, interprets -o with lower precedence than the implicit -a you have otherwise. Your original query was interpretted as:

find (dirs, those named "example") OR (files named "docs", and delete them)

PS: For your future convenience, shellcheck automatically warns many things like this:

In yourscript line 1:
find ./myfolder -type d -name example -o -name docs -exec rm -rf {} +
                                                    ^-- SC2146: This action ignores everything before the -o. Use \( \) to group.

Upvotes: 2

Related Questions