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

find . -name '*.js'

当前回答

对于那些在旧版本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

其他回答

对于那些在旧版本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 . -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

我在这个页面上找到了建议,很多其他页面在我的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分钟。如果没有这些例外,这需要很多小时。

#linux中的find命令def:find命令用于在unix/linux系统中查找/搜索文件,查找搜索目录层次结构中的文件

1) exec显示与-exec、-execdir、-ok和-okdir相关的诊断信息2) -选项-H=除非在处理过程中,否则不要遵循符号链接。-L=遵循符号链接-P=从不遵循符号链接-c型文件类型为c:b块(缓冲)特殊c字符(无缓冲)特殊d目录p命名管道(FIFO)f常规文件l符号链接;如果-L选项或follow选项有效,则永远不会出现这种情况,除非符号链接断开。如果要在-L有效时搜索符号链接,请使用-xtype。s插座D门(Solaris)-删除删除文件;如果删除成功,则为true。如果删除失败,将发出错误消息。如果-删除#如果失败,find的退出状态将为非零(当它最终退出时)。find/home/mohan/a-mindepth 3-maxdepth 3-type f-name“*.txt”|xargs rm-rffind-type d-namefind-type f-名称find/path/-type f-iname(i是大小写限制)#查找目录a/b/c,只有删除其中的c目录才有“*.txt”find/home/mohan/a-mindepth 3-maxdepth 3-type f-name“*.txt”|xargs rm-rffind/home/mohan/a-mindepth 3-maxdepath 3-type f-name“*.txt”-delete#删除特定目录有空文件,只有我们才能删除空文件find/home/mohan-type f-name“*.txt”-空-删除#查找多个文件,同时查找空文件find/home/mohan-type f \(-name“*.sh”-o-name“*.txt”\)-空#删除空文件两个或多个文件find/home/mohan-type f \(-name“*.sh”-o-name“*.txt”\)-empty-delete#如何将多个文件的内容追加到一个文件中查找-键入f-name“*.txt”-exec cat{}+>>output.file#上次修改的文件查找时间少于1分钟(-n)ls-lrth|查找-f-mmin-1型#上次修改的文件超过1分钟(+n)ls-lrth|查找-f型-mmin+1#上次修改的文件正好一分钟查找-f-mmin 1型使用命令(-mtime)在一天内完全修改最后一个文件查找-类型f-mtime 10#上次修改时间超过10天查找-类型f-mtime+10#上次修改时间少于10天查找-类型f-mtime-10#如何查找从给定日期到最新日期的修改文件和文件夹查找-类型f-newermt“17-11-2020”#如何查找过去30天内访问的“sh”扩展文件列表---matdimtypels-lrt |查找-类型f-iname“.sh”-atime-30#如何查找今天创建的文件列表,-1表示少于分钟,ls-lrt |查找-类型f-ctime-1-ls

find -name '*.js' -not -path './node_modules/*' -not -path './vendor/*'

似乎与

find -name '*.js' -not \( -path './node_modules/*' -o -path './vendor/*' \)

并且更容易记住IMO。