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

find . -name '*.js'

当前回答

这适合我在Mac上使用:

find . -name *.php -or -path "./vendor" -prune -or -path "./app/cache" -prune

它将排除带有php后缀的搜索名称的供应商和app/cache-dir。

其他回答

这是唯一一个对我有用的。

find / -name MyFile ! -path '*/Directory/*'

正在搜索“MyFile”,不包括“Directory”。强调星星*。

如果搜索目录有模式(在大多数情况下);您可以简单地如下所示:

find ./n* -name "*.tcl" 

在上述示例中;它搜索所有以“n”开头的子目录。

一个选项是使用grep排除包含目录名的所有结果。例如:

find . -name '*.js' | grep -v excludeddir

我认为自己是一个狂欢爱好者,但是。。。在过去的两年中,我们没有找到一个适合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。。。

find . -name '*.js' -\! -name 'glob-for-excluded-dir' -prune