linux find type -f还返回匹配目录中的不匹配文件

时间:2017-07-09 09:55:32

标签: linux find

当前目录中的所有文件/文件夹:

./color_a.txt   
./color_b.txt   
./color_c.txt   
./color/color_d.txt   
./color/blue.txt   
./color/red.txt   
./color/yellow.txt

命令用于查找名称中 color 的所有文件:

find ./*color* -type f

结果:

./color_a.txt   
./color_b.txt   
./color_c.txt   
./color/color_d.txt   
./color/blue.txt   
./color/red.txt   
./color/yellow.txt  

预期结果:

./color_a.txt   
./color_b.txt   
./color_c.txt    
./color/color_d.txt    

结果还包括匹配的父目录下的所有不匹配的文件名。 我怎样才能获得名称与 color 模式直接匹配的文件?

非常感谢!

2 个答案:

答案 0 :(得分:1)

您可能想要的文件名过滤是一个简单的 paper wood plastic fire water house 测试:

-name <glob-pattern>

来自find -name '*color*' -type f

man find

正如旁注,当你写道:

 -name pattern
        Base  of  file  name  (the  path with the leading directories removed) matches shell
        pattern pattern.  Because the leading directories are removed, the file names
        considered for a match with -name will never include a  slash,  so `-name a/b' will 
        never match anything (you probably need to use -path instead).

shell扩展了(未引用的)glob模式find ./*color* -type f ,真正执行的内容(./*color*看到的内容)是这样的:

find

因此在所有这些位置产生一个文件列表。

答案 1 :(得分:0)

您可以使用正则表达式选项

find -regex ".*color_.*" -type f