【发布时间】:2017-12-13 17:59:54
【问题描述】:
我编写了一个查找函数,它在给定路径中的每个文件中搜索字符串,同时跳过我不想搜索的目录名称列表。我已将此脚本放在我的 .bashrc 文件中,以便像这样调用:
findTEXTinFILES /path/to/search 'text-to-find'
查找部分效果很好,它为搜索文本着色,使其在视觉上脱颖而出!但我无法让它跳过使用 -prune 列出的目录。我已经阅读了所有我能找到的帖子,但没有一个对我有用。我尝试了多种变体,但没有运气。所以我有几个问题:
- 如何跳过多个目录?
- 如何跳过仅包含部分名称的目录,例如以“--”或“wp-”开头的目录?
- 能否在同一个脚本中混合使用
-name和-path条件? - 我还有什么遗漏的吗?
我的服务器是带有 bash shell 的 CENTOS 6.9 virtuozzo。
function findTEXTinFILES {
find "$1" ! \( -name .bash_history -prune \
-o ! -path tmp -prune \
-o ! -path short -prune \
-o ! -path "*/_not_used/*" -prune \
-o ! -path backups -prune \
-o ! -path temp_logs -prune \
-o ! -name .cpan -prune \
-o ! -name .cpobjcache -prune \
-o ! -path files_to_compare -prune \
-o ! -path logs -prune \
-o ! -path mail -prune \
-o ! -path old -prune \
-o ! -path '--*' -prune \
-o ! -path 'wp-*' -prune \
-o ! -path '*copy*' -prune \) \
-o -name "*" \
-exec grep $2 -I --color -Hn '$3' '{}' 2>/dev/null \;
}
【问题讨论】:
标签: linux text grep directory find