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

sh/bash/dash/ksh/zsh等Shell脚本
回复
alober
帖子: 144
注册时间: 2010-07-13 17:04

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

#1

帖子 alober » 2011-10-12 12:17

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

请各位不吝赐教。
上次由 alober 在 2011-10-12 13:24,总共编辑 1 次。
头像
Methuselar
帖子: 122
注册时间: 2009-06-04 12:06
联系:

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

#2

帖子 Methuselar » 2011-10-12 12:39

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$'
Mea Culpa!
头像
lilydjwg
论坛版主
帖子: 4258
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

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

#3

帖子 lilydjwg » 2011-10-12 12:48

为什么不用

代码: 全选

find -type f \( -name main -o -name '*.o' \)
alober
帖子: 144
注册时间: 2010-07-13 17:04

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

#4

帖子 alober » 2011-10-12 13:23

原来如此,我忽略了路径也占字符串的问题。
非常感谢楼上两位赐教。
回复