使用 find 命令实现高级排除需求

使用 find 命令实现高级排除需求

Linked

关于 find 命令,本博客介绍过 atime,ctime,mtime 介绍过 --exec 参数
介绍这些的基本需求是进行文件管理。事实上,基于终端进行 Linux 中排除掉筛选的文件 似乎有些许局限。
那么,如果希望据说强大的文件搜寻与处理功能。我们还得回到 find 命令上来,本文基于 "--prune" 参数进行说明。

Deleted

# find . -name "*git*"
./themes/snippet/layout/_vendor/comments/gitment.ejs
./themes/landscape/.gitignore
./.gitignore
./.git

这样我们可以找到所有带 "git" 的文件或者文件夹。更多细节可查询附录内 "find 查询参数" 一节。

Judge

find 命令本身是带条件判定的。在早期写脚本的时候由于不知道这个技巧,于是采用输出信息行数判断以确定是否搜索到信息。

在 find 命令中,-o 表示前者执行成功后者不执行;'-a' 表示前者执行成功后者执行。

# find . -name "*git*" -prune -a -printf "该条包含 git 信息
" 
该条包含 git 信息
该条包含 git 信息
该条包含 git 信息
该条包含 git 信息
# find . -name "*git*" -prune -a -print 
./themes/snippet/layout/_vendor/comments/gitment.ejs
./themes/landscape/.gitignore
./.gitignore
./.git
# find . -name "*git*" -prune -o -printf "该条不包含 git 信息
"

使用这条命令也就实现了高级排除的需求。

Appendix

find 查询参数细节

# find . -name "*git*" -type f
./themes/snippet/layout/_vendor/comments/gitment.ejs
./themes/landscape/.gitignore
./.gitignore
# find . -name "*git*" -type d
./.git
# find . -path "./.git"
./.git

**tips: ** '-name "string" -type d' 和直接 '-path "Relative_directory/directory"'。
**Attention: ** 使用 '-path' 后跟的目录必须是相对地址并且在尾部不可以加斜杠,且不可使用通配符。

原文地址:https://www.cnblogs.com/itxdm/p/Use_the_find_command_to_achieve_advanced_exclusion_requirements.html