使用find搜索*.js文件时,如何排除特定目录?
find . -name '*.js'
使用find搜索*.js文件时,如何排除特定目录?
find . -name '*.js'
当前回答
如何在sh中使用find的prune选项是Laurence Gonsalves关于prune如何工作的一个很好的答案。
下面是通用解决方案:
find /path/to/search \
-type d \
\( -path /path/to/search/exclude_me \
-o \
-name exclude_me_too_anywhere \
\) \
-prune \
-o \
-type f -name '*\.js' -print
要避免多次键入/path/To/seach/,请将查找包装在pushd中。。popd对。
pushd /path/to/search; \
find . \
-type d \
\( -path ./exclude_me \
-o \
-name exclude_me_too_anywhere \
\) \
-prune \
-o \
-type f -name '*\.js' -print; \
popd
其他回答
使用-prune主键。例如,如果要排除/其他:
find . -path ./misc -prune -o -name '*.txt' -print
要排除多个目录,请在括号中对它们进行“或”运算。
find . -type d \( -path ./dir1 -o -path ./dir2 -o -path ./dir3 \) -prune -o -name '*.txt' -print
而且,要在任何级别排除具有特定名称的目录,请使用-name primary而不是-path。
find . -type d -name node_modules -prune -o -name '*.json' -print
不确定这是否能涵盖所有边缘情况,但以下内容将非常简单明了:
ls-1 | grep-v-e ddl-e docs | xargs rm-rf
这将从当前目录excpet“ddls”和“docs”中删除所有文件/目录。
如果-prune对你不起作用,这将:
find -name "*.js" -not -path "./directory/*"
注意:需要遍历所有不需要的目录。
要排除多个目录,请执行以下操作:
find . -name '*.js' -not \( -path "./dir1" -o -path "./dir2/*" \)
要添加目录,请添加-o-path“./dirname/*”:
find . -name '*.js' -not \( -path "./dir1" -o -path "./dir2/*" -o -path "./dir3/*"\)
但是,如果有许多目录要排除,也许您应该使用正则表达式。
对于工作解决方案(在Ubuntu 12.04(精确穿山甲)上测试)。。。
find ! -path "dir1" -iname "*.mp3"
将在当前文件夹和子文件夹(dir1子文件夹除外)中搜索MP3文件。
Use:
find ! -path "dir1" ! -path "dir2" -iname "*.mp3"
…排除dir1和dir2