我有下面的find命令,我惊讶地看到.git目录正在被找到。为什么?
$ find . ! -name '*git*' | grep git
./.git/hooks
./.git/hooks/commit-msg
./.git/hooks/applypatch-msg.sample
./.git/hooks/prepare-commit-msg.sample
./.git/hooks/pre-applypatch.sample
./.git/hooks/commit-msg.sample
./.git/hooks/post-update.sample发布于 2013-10-18 22:36:17
因为“查找”搜索文件,而发现的任何文件的名称中都没有搜索模式(请参阅手册页)。您需要通过-prune开关删除违规目录:
find . -path ./.git -prune -o -not -name '*git*' -print |grep git请参阅Exclude directory from find . command
在没有-prune的情况下编辑另一个选项(以及更自然的imho):
find . -not -path "*git*" -not -name '*git*' |grep git发布于 2013-10-18 22:33:16
你只是看到了find的预期行为。-name测试只应用于文件名本身,而不是整个路径。如果您想搜索除.git目录之外的所有内容,可以使用bash(1)的extglob选项:
$ shopt -s extglob
$ find !(.git)发布于 2013-10-18 22:34:15
它找不到那些git文件。相反,它会在./..git/下面找到与模式! -name '*git*'匹配的文件,该模式包含所有在文件名中不包括git的文件(而不是路径名)。
查找-name是关于文件,而不是路径。
尝试-iwholename而不是-name
find . ! -iwholename '*git*'
https://stackoverflow.com/questions/19459961
复制相似问题