带有find -name命令的通配符和双引号

时间:2016-04-02 16:29:10

标签: linux command-line

双引号(和单引号)是否会抑制通配符扩展?如果是这样,那么为什么以下

find -name "d*"

没有使用通配符*?也就是说,上述命令的输出都是以字母d开头的文件和目录的路径,就好像正在进行一些扩展一样。我期待通配符被抑制,因此它只会输出到名为d*的文件的路径,但它不是。

这是否意味着双引号在某些情况下会有例外情况?

谢谢!

3 个答案:

答案 0 :(得分:3)

引号取消shell 的通配符扩展,这意味着在这种情况下find -name的参数将是文字d*

如果没有引号,shell将是扩展通配符的那个,在这种情况下,d*将扩展为当前目录中以d开头的所有名称的列表,或者{{1如果没有这样的文件。

d*搜索条件采用模式作为参数,语法与shell globs相同。在这种情况下,模式旨在由find解释,而不是由shell解释,因此需要引用。

要搜索文字-name,您可以将其括在方括号中:

*

或用反斜杠转义:

find -name "d[*]"

答案 1 :(得分:2)

在* NIX平台上,shell负责解释和扩展文件名通配符,甚至在执行被调用程序之前;程序只能看到扩展的文件名。

您可以通过

来抑制通配符扩展
$ # Using single or double quotes around it
$ find . -name '*'

$ # Escaping it
$ find . -name \*

$ # Or disable the glob expansion (noglob)
$ set -f
$ find . -name *

如果find-name参数需要模式

  -name pattern
              Base  of file name (the path with the leading directories
              removed) matches shell pattern pattern...
              Don't forget to enclose the pattern in quotes in order to
              protect it from expansion by the shell.

因此,即使shell没有扩展通配符,find也会在搜索匹配文件时使用您的表达式。

答案 2 :(得分:1)

澄清shell扩展,

方式1:使用 echo 命令作为参数使用这些模式。

way 2 :在执行find命令之前,执行 set -x 以启用查看哪些参数传递给命令“find”(或任何其他命令)