使用find搜索*.js文件时,如何排除特定目录?
find . -name '*.js'
使用find搜索*.js文件时,如何排除特定目录?
find . -name '*.js'
当前回答
TLDR:了解您的根目录,然后使用-path<excluded_path>-prine-o选项定制搜索。不要在排除路径的末尾包含尾随/。
例子:
find/-path/mnt-sprune-o-name“*libname-server-2.a*”-print
为了有效地使用find,我认为必须充分了解文件系统目录结构。在我的家用电脑上,我有多TB的硬盘,其中大约一半的内容使用rsnapshot(即rsync)进行备份。虽然备份到物理上独立(重复)的驱动器,但它安装在我的系统根目录(/)下:/mnt/Backups/rsnapshot_Backups/:
/mnt/Backups/
└── rsnapshot_backups/
├── hourly.0/
├── hourly.1/
├── ...
├── daily.0/
├── daily.1/
├── ...
├── weekly.0/
├── weekly.1/
├── ...
├── monthly.0/
├── monthly.1/
└── ...
/mnt/Backups/rsnapshot_Backups/目录当前占用约2.9 TB,包含约60M个文件和文件夹;简单地遍历这些内容需要时间:
## As sudo (#), to avoid numerous "Permission denied" warnings:
time find /mnt/Backups/rsnapshot_backups | wc -l
60314138 ## 60.3M files, folders
34:07.30 ## 34 min
time du /mnt/Backups/rsnapshot_backups -d 0
3112240160 /mnt/Backups/rsnapshot_backups ## 3.1 TB
33:51.88 ## 34 min
time rsnapshot du ## << more accurate re: rsnapshot footprint
2.9T /mnt/Backups/rsnapshot_backups/hourly.0/
4.1G /mnt/Backups/rsnapshot_backups/hourly.1/
...
4.7G /mnt/Backups/rsnapshot_backups/weekly.3/
2.9T total ## 2.9 TB, per sudo rsnapshot du (more accurate)
2:34:54 ## 2 hr 35 min
因此,每当我需要在我的/(根)分区上搜索文件时,我都需要处理(如果可能的话)遍历我的备份分区。
示例
在本主题中提出的各种方法(如何在find.command中排除目录)中,我发现使用公认的答案进行搜索要快得多,但需要注意。
解决方案1
假设我想查找系统文件libname-server-2.a,但不想搜索rsnapshot备份。要快速查找系统文件,请使用排除路径/mnt(即,使用/mnt,而不是/mnt/,或/mnt/Backups,或…):
## As sudo (#), to avoid numerous "Permission denied" warnings:
time find / -path /mnt -prune -o -name "*libname-server-2.a*" -print
/usr/lib/libname-server-2.a
real 0m8.644s ## 8.6 sec <<< NOTE!
user 0m1.669s
sys 0m2.466s
## As regular user (victoria); I also use an alternate timing mechanism, as
## here I am using 2>/dev/null to suppress "Permission denied" warnings:
$ START="$(date +"%s")" && find 2>/dev/null / -path /mnt -prune -o \
-name "*libname-server-2.a*" -print; END="$(date +"%s")"; \
TIME="$((END - START))"; printf 'find command took %s sec\n' "$TIME"
/usr/lib/libname-server-2.a
find command took 3 sec ## ~3 sec <<< NOTE!
…在几秒钟内找到该文件,而这需要更长的时间(似乎在所有“排除”目录中重复出现):
## As sudo (#), to avoid numerous "Permission denied" warnings:
time find / -path /mnt/ -prune -o -name "*libname-server-2.a*" -print
find: warning: -path /mnt/ will not match anything because it ends with /.
/usr/lib/libname-server-2.a
real 33m10.658s ## 33 min 11 sec (~231-663x slower!)
user 1m43.142s
sys 2m22.666s
## As regular user (victoria); I also use an alternate timing mechanism, as
## here I am using 2>/dev/null to suppress "Permission denied" warnings:
$ START="$(date +"%s")" && find 2>/dev/null / -path /mnt/ -prune -o \
-name "*libname-server-2.a*" -print; END="$(date +"%s")"; \
TIME="$((END - START))"; printf 'find command took %s sec\n' "$TIME"
/usr/lib/libname-server-2.a
find command took 1775 sec ## 29.6 min
解决方案2
本线程中提供的其他解决方案(SO#4210042)也表现不佳:
## As sudo (#), to avoid numerous "Permission denied" warnings:
time find / -name "*libname-server-2.a*" -not -path "/mnt"
/usr/lib/libname-server-2.a
real 33m37.911s ## 33 min 38 sec (~235x slower)
user 1m45.134s
sys 2m31.846s
time find / -name "*libname-server-2.a*" -not -path "/mnt/*"
/usr/lib/libname-server-2.a
real 33m11.208s ## 33 min 11 sec
user 1m22.185s
sys 2m29.962s
总结|结论
使用“解决方案1”中所示的方法
find / -path /mnt -prune -o -name "*libname-server-2.a*" -print
即
... -path <excluded_path> -prune -o ...
请注意,每当您将尾随/添加到排除路径时,find命令就会递归地输入(所有这些)/mnt/*目录——在我的情况下,由于/mnt/Backups/rsnapshot_Backups/*子目录,该目录还包含约2.9 TB的文件要搜索!通过不附加尾随/,搜索应该几乎立即完成(几秒钟内)。
“解决方案2”(…-not-path<exclude-path>…)似乎同样递归地搜索排除的目录——不返回排除的匹配项,但不必要地消耗搜索时间。
在这些rsnapshot备份中搜索:
要在每小时/每天/每周/每月的rsnapshot备份中查找文件,请执行以下操作:
$ START="$(date +"%s")" && find 2>/dev/null /mnt/Backups/rsnapshot_backups/daily.0 -name '*04t8ugijrlkj.jpg'; END="$(date +"%s")"; TIME="$((END - START))"; printf 'find command took %s sec\n' "$TIME"
/mnt/Backups/rsnapshot_backups/daily.0/snapshot_root/mnt/Vancouver/temp/04t8ugijrlkj.jpg
find command took 312 sec ## 5.2 minutes: despite apparent rsnapshot size
## (~4 GB), it is in fact searching through ~2.9 TB)
排除嵌套目录:
在这里,我想排除嵌套目录,例如,当从/mnt/VVancouver/projects/ie/calls/data/*搜索/mnt/Vvancouver/products/时
$ time find . -iname '*test_file*'
./ie/claws/data/test_file
./ie/claws/test_file
0:01.97
$ time find . -path '*/data' -prune -o -iname '*test_file*' -print
./ie/claws/test_file
0:00.07
旁白:在命令末尾添加-print将抑制排除目录的打印输出:
$ find / -path /mnt -prune -o -name "*libname-server-2.a*"
/mnt
/usr/lib/libname-server-2.a
$ find / -path /mnt -prune -o -name "*libname-server-2.a*" -print
/usr/lib/libname-server-2.a
其他回答
我在这个页面上找到了建议,很多其他页面在我的Mac OS X系统上都不起作用。然而,我发现了一种对我有用的变体。
大的想法是搜索Macintosh HD,但避免遍历所有外部卷,这些卷主要是Time Machine备份、映像备份、装载的共享和存档,但不必全部卸载,这通常是不切实际的。
这是我的工作脚本,我将其命名为“findit”。
#!/usr/bin/env bash
# inspired by http://stackoverflow.com/questions/4210042/exclude-directory-from-find-command Danile C. Sobral
# using special syntax to avoid traversing.
# However, logic is refactored because the Sobral version still traverses
# everything on my system
echo ============================
echo find - from cwd, omitting external volumes
date
echo Enter sudo password if requested
sudo find . -not \( \
-path ./Volumes/Archive -prune -o \
-path ./Volumes/Boot\ OS\ X -prune -o \
-path ./Volumes/C \
-path ./Volumes/Data -prune -o \
-path ./Volumes/jas -prune -o \
-path ./Volumes/Recovery\ HD -prune -o \
-path ./Volumes/Time\ Machine\ Backups -prune -o \
-path ./Volumes/SuperDuper\ Image -prune -o \
-path ./Volumes/userland -prune \
\) -name "$1" -print
date
echo ============================
iMac2:~ jas$
不同的路径与外部存档卷、Time Machine、虚拟机、其他装载的服务器等有关。一些卷名中有空格。
一个好的测试运行是“finditindex.php”,因为该文件出现在我的系统中的许多地方。使用此脚本,搜索主硬盘大约需要10分钟。如果没有这些例外,这需要很多小时。
这是因为find测试模式“*foo*”的文件:
find ! -path "dir1" ! -path "dir2" -name "*foo*"
但如果不使用模式(find不测试文件),它就不起作用。因此find没有使用它以前评估的“true”和“false”布尔值。不使用上述符号的用例示例:
find ! -path "dir1" ! -path "dir2" -type f
没有找到测试!因此,如果您需要查找没有任何模式匹配的文件,请使用-prune。此外,通过使用prune查找总是更快,因为它确实跳过了该目录,而不是匹配它或更好地不匹配它
find dir -not \( -path "dir1" -prune \) -not \( -path "dir2" -prune \) -type f
or:
find dir -not \( -path "dir1" -o -path "dir2" -prune \) -type f
当做
这是唯一一个对我有用的。
find / -name MyFile ! -path '*/Directory/*'
正在搜索“MyFile”,不包括“Directory”。强调星星*。
我认为自己是一个狂欢爱好者,但是。。。在过去的两年中,我们没有找到一个适合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。。。
这适合我在Mac上使用:
find . -name *.php -or -path "./vendor" -prune -or -path "./app/cache" -prune
它将排除带有php后缀的搜索名称的供应商和app/cache-dir。