使用find搜索*.js文件时,如何排除特定目录?

find . -name '*.js'

当前回答

对于我所需要的,它是这样工作的,从root开始在所有服务器中查找landscape.jpg,不包括/var目录中的搜索:

find/-maxdepth 1-type d | grep-v/var | xargs-I“{}”find“{}”-name landscape.jpg

find/-maxdepth 1-type d列出/

grep-v/var从列表中排除“/var”

xargs-I“{}”find“{}”-name landscape.jpg执行任何命令,如find with each directory/result from list

其他回答

path-prune方法还可以处理路径中的通配符。下面是一个find语句,它将查找服务于多个git存储库的git服务器的目录,而不包括git内部目录:

find . -type d \
   -not \( -path */objects -prune \) \
   -not \( -path */branches -prune \) \
   -not \( -path */refs -prune \) \
   -not \( -path */logs -prune \) \
   -not \( -path */.git -prune \) \
   -not \( -path */info -prune \) \
   -not \( -path */hooks -prune \)  

我认为自己是一个狂欢爱好者,但是。。。在过去的两年中,我们没有找到一个适合bash用户的解决方案。我所说的“用户友好”是指只需一次调用,这不需要我记住复杂的语法+我可以使用与以前相同的find语法,因此以下解决方案最适合那些^^^

复制粘贴到shell中,并将~/.bash_aliases作为源代码:

cat << "EOF" >> ~/.bash_aliases
# usage: source ~/.bash_aliases , instead of find type findd + rest of syntax
findd(){
   dir=$1; shift ;
   find  $dir -not -path "*/node_modules/*" -not -path "*/build/*" \
      -not -path "*/.cache/*" -not -path "*/.git/*" -not -path "*/venv/*" $@
}
EOF

当然,为了添加或删除要排除的目录,您必须使用您选择的目录编辑此别名func。。。

对于那些在旧版本UNIX上无法使用-path或-not的用户

在SunOS 5.10 bash 3.2和SunOS 5.11 bash 4.4上测试

find . -type f -name "*" -o -type d -name "*excluded_directory*" -prune -type f

使用-prune选项。因此,类似于:

find . -type d -name proc -prune -o -name '*.js'

“-typed-name-proc-prune”只查找要排除的名为proc的目录。“-o”是“OR”运算符。

这是我用来排除某些路径的格式:

$ find ./ -type f -name "pattern" ! -path "excluded path" ! -path "excluded path"

我使用此命令查找不在“.*”路径中的所有文件:

$ find ./ -type f -name "*" ! -path "./.*" ! -path "./*/.*"