Unix - 使用find列出所有.html文件。 (不要使用shell通配符或ls命令)

时间:2013-09-17 15:00:04

标签: regex shell unix find

我已尝试'find -name .html$''find -name .html\>' 没有工作。

我想知道为什么这两个是错的,什么是没有通配符的正确使用?

4 个答案:

答案 0 :(得分:4)

find . -name '*.html'

你必须单引号通配符以防止shell在传递它时将其全局化。

答案 1 :(得分:4)

你需要的是

find -name '*.html'

或者正则表达式:

find -regex '.*/.*\.html'

要忽略大小写,请使用-iname或-iregex:

find -iname '*.html'
find -iregex '.*/.*\.html'

-name的手册:

   -name pattern
          Base of file name (the path with the leading directories
          removed) matches shell pattern pattern.  The metacharacters
          (`*', `?', and `[]') match a `.' at the start of the base name
          (this is a change in findutils-4.2.2; see section STANDARDS CON‐
          FORMANCE 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.

答案 2 :(得分:1)

你想要

find . -name "*.html"

查找默认情况下使用emacs regex,而不是您可能习惯使用的posix。

答案 3 :(得分:1)

你在这里错过了几件事。首先是路径。如果要在本地路径中搜索,请使用.例如:find .将在当前目录中以递归方式列出每个文件和目录。第二个*是通配符。因此,要查找当前目录中的所有.html文件,请尝试

find . -name *.html