How to exclude a directory in find . command

后端 未结 30 1366
醉酒成梦
醉酒成梦 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条回答
  •  渐次进展
    2020-11-22 04:13

    There are plenty of good answers, it just took me some time to understand what each element of the command was for and the logic behind it.

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

    find will start finding files and directories in the current directory, hence the find ..

    The -o option stands for a logical OR and separates the two parts of the command :

    [ -path ./misc -prune ] OR [ -name '*.txt' -print ]
    

    Any directory or file that is not the ./misc directory will not pass the first test -path ./misc. But they will be tested against the second expression. If their name corresponds to the pattern *.txt they get printed, because of the -print option.

    When find reaches the ./misc directory, this directory only satisfies the first expression. So the -prune option will be applied to it. It tells the find command to not explore that directory. So any file or directory in ./misc will not even be explored by find, will not be tested against the second part of the expression and will not be printed.

提交回复
热议问题