Skip to content
Advertisement

linux find command: exclude path from result

I used the find command to find all python files in a folder, and exlude some folder.

find . -type d ( -path ./venv -o -path ./virtualenv -o -path ./lib ) -prune -o -iname '*.py'

It’s give the following output:

...
./tests/test_a.py
./venv

How can I tweak the find command to exclude ./venv in the resultset?

Advertisement

Answer

If you don’t specify any actions, find will implicitly use -print for any file that matches your expression. -prune always evaluates as true (in addition to preventing further recursion), which is why ./venv still ends up being printed.

To avoid that, simply add a -print to your other branch:

find . -type d ( -path ./venv -o -path ./virtualenv -o -path ./lib ) -prune 
       -o -iname '*.py' -print

Now there’s an action, so find no longer prints everything that happens to evaluate as true, and instead only prints the files you’re explicitly looking for.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement