分页: 1 / 1

请教一个 find 用正则查找文件的问题(已解决)

发表于 : 2011-10-12 12:17
alober
我想通过 find 查找目录中所有以 .o 结尾的文件和一个名为 main 的文件,这个 find 应该如何写?我写下边的表达式只能找到 .o 但无法找到那个 main。
find . -regex "\(.*\.\(o\)\)\|\(main\)" -ls

请各位不吝赐教。

Re: 请教一个 find 用正则查找文件的问题

发表于 : 2011-10-12 12:39
Methuselar
man find 可得:
-regex pattern
File name matches regular expression pattern. This is a match on the whole path, not a search. For example, to match a file named `./fubar3', you can use the regular expression `.*bar.' or `.*b.*3', but not `b.*r3'.
所以你的main要匹配全路径, 也就是你find -type f -name main的输出,一字不差.

代码: 全选

find . -type f -regex '.*\.o$\|.*/main$'

Re: 请教一个 find 用正则查找文件的问题

发表于 : 2011-10-12 12:48
lilydjwg
为什么不用

代码: 全选

find -type f \( -name main -o -name '*.o' \)

Re: 请教一个 find 用正则查找文件的问题

发表于 : 2011-10-12 13:23
alober
原来如此,我忽略了路径也占字符串的问题。
非常感谢楼上两位赐教。