使用正则表达式查找文件名中没有下划线的文件

时间:2016-06-26 21:07:28

标签: regex linux find

在目录下,文件名中有下划线的文件。我想获取这些文件并将其打印在屏幕上。 我使用的命令是:

find ./ -type f -name '[a-z0-9]*\.java'

但我仍然可以在屏幕上显示文件./a_b.java。 你能告诉我我的正则表达式有什么问题吗?

2 个答案:

答案 0 :(得分:0)

它不是正则表达式,它是一种模式。 *表示"任何字符",如ls *。使用-regex代替-name来实际使用正则表达式,但请注意它必须匹配整个路径,而不仅仅是文件名。

find . -type f -regex '.*/[a-z0-9]*\.java'

答案 1 :(得分:0)

-name中的

findglob模式。如果您不确定,请查看手册页:

   -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).  A warning is issued if you try to do  this,  unless  the  environment  variable  POSIXLY_CORRECT  is  set.   The
          metacharacters  (`*',  `?',  and  `[]') match a `.' at the start of the base name (this is a change in findutils-4.2.2; see section STANDARDS
          CONFORMANCE below).  To ignore a directory and the files under it, use -prune; see an example in the description of -path.   Braces  are  not
          recognised  as  being  special,  despite the fact that some shells including Bash imbue braces with a special meaning in shell patterns.  The
          filename matching is performed with the use of the fnmatch(3) library function.   Don't forget to enclose the pattern in quotes in  order  to
          protect it from expansion by the shell.