为什么找到. -not -name“.*"不排除隐藏文件? [英] Why does find . -not -name ".*" not exclude hidden files?
问题描述
我想忽略所有隐藏文件,尤其是在搜索(以及以后替换)文件时忽略.git和.svn文件,不是我发现许多在线教程中介绍的排除此类隐藏文件的最基本方法并没有在这里工作.
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.
find . -not -name ".*"
还将打印隐藏文件.
我要编写的脚本是
replace() {
if [ -n "$3" ]; then expr="-name \"$3\""; fi
find . -type f \( $expr -not -name ".*" \) -exec echo sed -i \'s/$1/$2/g\' {} \;
unset expr
}
推荐答案
问题是-not -name ".*"
确实匹配以."开头的所有文件和目录. -但它不会从搜索中删除它们,因此您将从隐藏目录中获得匹配项.要修剪路径,请使用-prune
,即:
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.:
find $PWD -name ".*" -prune -o -print
(我使用$PWD
,因为否则搜索."的开始也将被删除,并且将没有输出)
(I use $PWD
because otherwise the start of the search "." would also be pruned and there would be no output)
这篇关于为什么找到. -not -name“.*"不排除隐藏文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!