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

Zoe*_*Zoe 3 regex unix shell find

我试过了'find -name .html$''find -name .html\>'
没有一个工作。

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

Bil*_*ill 5

find . -name '*.html'
Run Code Online (Sandbox Code Playgroud)

您必须将通配符用单引号引起来,以防止 shell 在传递通配符进行查找时将其通配。

  • 像“*.html”这样的命令行参数由 shell 解释。像“*.html”这样的命令行参数会未经解释地传递给命令。在本例中,“find”以与 shell 类似但不相同的方式使用“*”。作为一个更明显的例子,请考虑“echo '***HELLO!***'”。该命令中的“*”字符肯定不是通配符,它​​们只是“echo”的参数。 (3认同)

kon*_*box 5

你需要的是

find -name '*.html'
Run Code Online (Sandbox Code Playgroud)

或者对于正则表达式:

find -regex '.*/.*\.html'
Run Code Online (Sandbox Code Playgroud)

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

find -iname '*.html'
find -iregex '.*/.*\.html'
Run Code Online (Sandbox Code Playgroud)

手册-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.
Run Code Online (Sandbox Code Playgroud)