Search code examples
linuxshellfind

How do I exclude a directory when using `find`?


How do I exclude a specific directory when searching for *.js files using find?

find . -name '*.js'

Solution

  • Use the -prune primary. For example, if you want to exclude ./misc:

    find . -path ./misc -prune -o -name '*.txt' -print
    

    To exclude multiple directories, OR them between parentheses.

    find . -type d \( -path ./dir1 -o -path ./dir2 -o -path ./dir3 \) -prune -o -name '*.txt' -print
    

    And, to exclude directories with a specific name at any level, use the -name primary instead of -path.

    find . -type d -name node_modules -prune -o -name '*.json' -print