无法正确使用find -name

时间:2014-07-03 10:41:39

标签: regex linux bash find regex-greedy

  1. 是否有人可以举例说明如何使用find -name "pattern"命令,尤其是"pattern" s,其中使用“”作为正则表达式的一部分。因为我的模式在在线正则表达式中匹配,所以bit在'find'实用程序中不起作用。

  2. 有人可以通过示例解释这对于手册的含义是什么吗?

  3.   

    基本名称开头的元字符(*',?'和[]') match a。'(这是findutils-4.2.2中的更改;请参阅下面的STANDARDS CONFORMANCE部分)

    1. 如何正确使用"?"中的find -name "pattern"?我是否更正"?"表示“零或一个符号”?

2 个答案:

答案 0 :(得分:3)

来自find手册页:

  

-name 模式文件名的基础(删除了前导目录的路径)与 shell模式模式匹配。

您的问题的答案是术语 shell模式的定义,解释为here(您还可以在shell的手册页中找到解释):

* 匹配任何零个或多个字符。

匹配任何一个字符。

<强> [字符串]     正好匹配一个字符串字符串成员的字符。这称为字符类。作为简写,字符串可以包含范围,范围由两个字符组成,它们之间有一个破折号。例如,类'[a-z0-9_]'匹配小写字母,数字或下划线。你可以通过在开始括号后面放置一个'!'或'^'来否定一个类。因此,'[^ A-Z @]'匹配除大写字母或符号外的任何字符。

<强> \     删除其后面的字符的特殊含义。这甚至在字符类中也有效。


<强>实施例

我希望以下示例解释?*之间的区别。

touch foobar
touch fobar

# * matches the tail (finds 'foobar')
find -name 'foo*'

# * matches the head (finds both files)
find -name '*bar'

# ? matches the 'f' at start (finds 'foobar')
find -name '?oobar'

# ? DON'T matches the whole tail (finds nothing)
find -name 'foo?'

# ? matches the second 'o' (finds 'foobar')
find -name 'fo?bar'

# ? matches one instance of any character after the 'f' (finds 'fobar')
find -name 'f?bar'

# * matches both zero or more characters after 'o' (finds both files)
find -name 'fo*bar'

此示例显示如何使用括号运算符[string]

touch fobar
touch fubar

# matches a single 'o' OR 'u' (finds both files)
find -name 'f[ou]bar'

当然,你可以自由地混合各种元字符:

# Finds every file starting with an 'o' or 'u' (included files named 'o' or 'u')
find -name '[ou]*'

# Finds every file starting with an 'o' or 'u' (excluded files named 'o' or 'u')
find -name '[ou]?*'

答案 1 :(得分:0)

  1. 一个例子:

    [root @ v3~] #find -name'op?n * .sh'

    ./ openerp-install.sh

    [root @ v3~] #find -name'op?en * .sh'

    [root @ v3~]#

  2. basename是没有路径的文件的名称,例如,如果文件的完整路径是“/usr/share/pkgconfig/gnome-icon-theme.pc”,那么该文件的基本名称将是“侏儒图标-theme.pc“

  3. “?”在查找模式中意味着“恰好一个符号”,而不是“零或一个符号”