How to exclude a directory in find . command

后端 未结 30 1403
醉酒成梦
醉酒成梦 2020-11-22 03:36

I\'m trying to run a find command for all JavaScript files, but how do I exclude a specific directory?

Here is the find code we\'re using.<

30条回答
  •  -上瘾入骨i
    2020-11-22 04:12

    Use the -prune switch. For example, if you want to exclude the misc directory just add a -path ./misc -prune -o to your find command:

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

    Here is an example with multiple directories:

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

    Here we exclude ./dir1, ./dir2 and ./dir3 in the current directory, since in find expressions it is an action that acts on the criteria -path dir1 -o -path dir2 -o -path dir3 (if dir1 or dir2 or dir3), ANDed with type -d.

    To exclude directory name at any level, use -name:

    find . -type d \( -name node_modules -o -name dir2 -o -path name \) -prune -false -o -name '*.json'
    

提交回复
热议问题