bash 为什么找到 . -not -name ".*" 不排除隐藏文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16900675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does find . -not -name ".*" not exclude hidden files?
提问by user1273684
I want to ignore all hidden files, but especially .git and .svn ones when searching (and later replacing) files, not I have found that the most basic way to exclude such hidden files described in many online tutorials doesn't work here.
我想忽略所有隐藏文件,但在搜索(以及稍后替换)文件时尤其是 .git 和 .svn 文件,我发现许多在线教程中描述的排除此类隐藏文件的最基本方法在这里不起作用。
find . -not -name ".*"
will also print hidden files.
还将打印隐藏文件。
The script I'm trying to write is
我正在尝试编写的脚本是
replace() {
if [ -n "" ]; then expr="-name \"\""; fi
find . -type f \( $expr -not -name ".*" \) -exec echo sed -i \'s///g\' {} \;
unset expr
}
回答by Joni
The thing is -not -name ".*"does match all files and directories that start with anything but "." - but it doesn't prune them from the search, so you'll get matches from inside hidden directories. To prune paths use -prune, i.e.:
事情是-not -name ".*"匹配所有以“.”开头的文件和目录。- 但它不会从搜索中删除它们,因此您将从隐藏目录中获得匹配项。修剪路径使用-prune,即:
find $PWD -name ".*" -prune -o -print
(I use $PWDbecause otherwise the start of the search "." would also be pruned and there would be no output)
(我使用$PWD因为否则搜索“。”的开始也会被修剪并且不会有输出)
回答by user1273684
correct version
正确的版本
replace() {
if [ -n "" ]; then expr=-name\ ; fi
find $PWD -name '.*' -prune -o $expr -type f -exec sed -i s///g {} \;
unset expr
}

