我想通过 find 查找目录中所有以 .o 结尾的文件和一个名为 main 的文件,这个 find 应该如何写?我写下边的表达式只能找到 .o 但无法找到那个 main。
find . -regex "\(.*\.\(o\)\)\|\(main\)" -ls
请各位不吝赐教。
请教一个 find 用正则查找文件的问题(已解决)
-
- 帖子: 144
- 注册时间: 2010-07-13 17:04
请教一个 find 用正则查找文件的问题(已解决)
上次由 alober 在 2011-10-12 13:24,总共编辑 1 次。
- Methuselar
- 帖子: 122
- 注册时间: 2009-06-04 12:06
- 联系:
Re: 请教一个 find 用正则查找文件的问题
man find 可得:
所以你的main要匹配全路径, 也就是你find -type f -name main的输出,一字不差.-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'.
代码: 全选
find . -type f -regex '.*\.o$\|.*/main$'
Mea Culpa!
- lilydjwg
- 论坛版主
- 帖子: 4258
- 注册时间: 2009-04-11 23:46
- 系统: Arch Linux
- 联系:
Re: 请教一个 find 用正则查找文件的问题
为什么不用?
代码: 全选
find -type f \( -name main -o -name '*.o' \)
-
- 帖子: 144
- 注册时间: 2010-07-13 17:04
Re: 请教一个 find 用正则查找文件的问题
原来如此,我忽略了路径也占字符串的问题。
非常感谢楼上两位赐教。
非常感谢楼上两位赐教。