Troskyvs
Troskyvs

Reputation: 8047

How to exclude multiple subdirectories when using "find" command?

I was looking for "*.py" files, and exclude both "build" and "bin" directories. I used this command:

find * -path "build" -prune -path "bin" -prune -o -type f \( -name "*.py" \) -print > findpyfiles.txt

The "findpyfiles.txt" still contains results started with "bin/". How to fix it?

Upvotes: 0

Views: 82

Answers (1)

Armali
Armali

Reputation: 19375

Although the question has an answer elsewhere, it may be worth knowing why your command didn't work right: You omitted an operator between -path "build" -prune and -path "bin" -prune, and -and is assumed where the operator is omitted, thus the -path "bin" is not evaluated when -path "build" returns false. Explicitly specifying the OR operator fixes it, either

find * -path build -prune -o -path bin -prune -o -type f -name "*.py" -print >findpyfiles.txt

or

find * \( -path build -o -path bin \) -prune -o -type f -name "*.py" -print >findpyfiles.txt

Upvotes: 1

Related Questions