当尝试使用 find 命令修剪隐藏目录时,它似乎“修剪”了隐藏文件在顶级目录中?
在下面的代码中,我希望看到由 find 命令打印的 find_examples_out/.t1、find_examples_out/.t2 和 find_examples_out/.s1 文件,但由于某种原因它们被排除在外。它们很好地显示在子目录中。
测试脚本:
#!/bin/csh
# GNU find version 4.1.20
find -version
mkdir find_examples_out
cd find_examples_out
set FILES = (t1 .t1 t2 .t2 s1 .s1)
set DIRS = (.hidden normal notnormal another)
foreach f ( $FILES )
touch $f
end
foreach i ( $DIRS )
mkdir $i
cd $i
foreach f ( $FILES )
touch $f
end
cd ..
end
echo "Files present:"
ls -AR
echo
echo "Give me all files but exclude some paths:"
find . \
\( \
-path "\.?.*" \
-o -path "*normal*" \
\) \
-prune \
-o \
\
\( \
-type f \
\) \
-print
cd ..
rm -rf find_examples_out
这是输出:
GNU find version 4.1.20
Files present:
.:
another .hidden normal notnormal s1 .s1 t1 .t1 t2 .t2
./another:
s1 .s1 t1 .t1 t2 .t2
./.hidden:
s1 .s1 t1 .t1 t2 .t2
./normal:
s1 .s1 t1 .t1 t2 .t2
./notnormal:
s1 .s1 t1 .t1 t2 .t2
Give me all files but exclude some paths:
./t1
./t2
./s1
./another/t1
./another/.t1
./another/t2
./another/.t2
./another/s1
./another/.s1
我在这里缺少什么?
In the code below I would expect to see find_examples_out/.t1, find_examples_out/.t2 and find_examples_out/.s1 files printed by the find command but they are excluded for some reason. They show up in the sub directories just fine.
Test script:
#!/bin/csh
# GNU find version 4.1.20
find -version
mkdir find_examples_out
cd find_examples_out
set FILES = (t1 .t1 t2 .t2 s1 .s1)
set DIRS = (.hidden normal notnormal another)
foreach f ( $FILES )
touch $f
end
foreach i ( $DIRS )
mkdir $i
cd $i
foreach f ( $FILES )
touch $f
end
cd ..
end
echo "Files present:"
ls -AR
echo
echo "Give me all files but exclude some paths:"
find . \
\( \
-path "\.?.*" \
-o -path "*normal*" \
\) \
-prune \
-o \
\
\( \
-type f \
\) \
-print
cd ..
rm -rf find_examples_out
Here is the output:
GNU find version 4.1.20
Files present:
.:
another .hidden normal notnormal s1 .s1 t1 .t1 t2 .t2
./another:
s1 .s1 t1 .t1 t2 .t2
./.hidden:
s1 .s1 t1 .t1 t2 .t2
./normal:
s1 .s1 t1 .t1 t2 .t2
./notnormal:
s1 .s1 t1 .t1 t2 .t2
Give me all files but exclude some paths:
./t1
./t2
./s1
./another/t1
./another/.t1
./another/t2
./another/.t2
./another/s1
./another/.s1
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非我忽略了某些内容,否则 find 的 -path 开关会比较给定的路径(包括文件名)的模式。
因此,您的 -path "\.?.*" 开关将匹配隐藏文件“.t1”等。
FWIW:在我拥有的 find 版本(v4.4.2)中,参数-path 是 shell 模式,而不是正则表达式。不过,我使用 bash,从未使用过 csh,所以也许这也会有所不同。
编辑:我尝试将其添加为评论,但它不断破坏格式。
您可以使用它来实现(我认为)您想要实现的目标:
Unless I've overlooked something, the -path switch to find compares the pattern given to the path including the filename.
Ergo, your -path "\.?.*" switch will match the hidden files ".t1" etc.
FWIW: in the version of find that I have (v4.4.2), the argument to -path is a shell pattern, not a regex. However, I use bash and have never used csh, so perhaps that makes a difference too.
EDIT: I tried to add this as a comment, but it keeps destroying the formatting.
You could use this to achieve what (I think) you are trying to achieve: