find 命令在 bash shell 中和 -name 选项的疑惑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5672577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find command in bash shell and the -name option doubts
提问by Tracy
What is the difference between the two below:
下面两者有什么区别:
find . -type f -name \*.bmp
find . -type f -name *.bmp
I have tested,they both return the same result,so is there anything different _deep inside_?
我已经测试过,它们都返回相同的结果,那么有什么不同_deep inside_吗?
Added from the removed answer:
从删除的答案中添加:
So it is to avoid the shell expansion for the special ***** character,solely pass * as a argument to the find command and let it process it.
所以是为了避免特殊*****字符的shell扩展,只是将*作为参数传递给find命令并让它处理它。
But on my machine,they are all good, both return the bmp files in and below the current directory,to name a few,the result is like below,some are omitted for brevity
但是在我的机器上,它们都很好,都返回当前目录中和当前目录下的bmp文件,仅举几例,结果如下,为简洁起见省略了一些
./images/building_color.bmp
./images/building_gray.bmp
./images/car_gray.bmp
./images/temple_color.bmp
./images/boat_gray.bmp
./images/tools_gray.bmp
./images/temple_gray.bmp
./images/tools_color.bmp
./images/car_color.bmp
./images/boat_color.bmp
system info:
系统信息:
GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)
GNU bash,版本 4.1.5(1)-release (i486-pc-linux-gnu)
Linux sysabod-laptop 2.6.32-30-generic #59-Ubuntu SMP Tue Mar 1 21:30:21 UTC 2011 i686 GNU/Linux
Linux sysabod-laptop 2.6.32-30-generic #59-Ubuntu SMP Tue Mar 1 21:30:21 UTC 2011 i686 GNU/Linux
回答by Walter Mundt
Here's how they're different: the first one always works, and the second one doesn't.
以下是它们的不同之处:第一个总是有效,而第二个无效。
As for why: in bash, shell globs (wildcard patterns including * or ?) are expanded by the shell into all files matching the glob. However, if no such files exist, the pattern is left alone.
至于为什么:在 bash 中,shell globs(通配符模式,包括 * 或 ?)被 shell 扩展为与 glob 匹配的所有文件。但是,如果不存在此类文件,则该模式将被保留。
So, if you're in a directory with no bmpfiles, the commands work the same way, because the first is escaped and bash fails to find any files matching in the second case.
因此,如果您在一个没有bmp文件的目录中,命令的工作方式相同,因为第一个被转义,bash 无法在第二种情况下找到任何匹配的文件。
If you ran it from a directory containing only one such file, say foo.bmp, the first would find all bmpfiles in the subtree, while the second would find all files named foo.bmponly. If run in a directory with multiple bmpfiles, I believe you'll get an error because finddoesn't know what to do with all the filenames.
如果您从仅包含一个此类文件的目录运行它,例如foo.bmp,第一个将查找bmp子树中的所有文件,而第二个将查找所有foo.bmp仅命名的文件。如果在包含多个bmp文件的目录中运行,我相信您会收到错误消息,因为find不知道如何处理所有文件名。
回答by bmk
When you escape the asterisk (\*) the asterisk itself is passed as argument to the findcommand and will be evaluated by find. If you don't escape the asterisk (*) already the shell evaluates it and expands it to the file names matching the pattern.
当您转义星号 ( \*) 时,星号本身将作为参数传递给find命令,并由find. 如果您没有对星号 ( *) 进行转义,shell 会对其进行评估并将其扩展为与模式匹配的文件名。
Fore example consider following directory structure:
例如考虑以下目录结构:
./a.txt
./b.bmp
./c.bmp
./dir/d.doc
./dir/e.bmp
When you execute
当你执行
find . -type f -name *.bmp
the shell expands *.bmpto b.bmp c.bmp. I.e. the command that is actually executed will be:
外壳扩展*.bmp到b.bmp c.bmp. 即实际执行的命令将是:
find . -type f -name b.bmp c.bmp
which will find b.bmpand c.bmpbut not dir/e.bmp.
哪个会找到b.bmpandc.bmp但不是dir/e.bmp.
When you execute
当你执行
find . -type f -name \*.bmp
*.bmpis passed directly as it is to find. findwill recurse through the current directory (.) and all its subdirectories (in the example only dir) and will find all files in those directories matching the pattern. The result will be: b.bmp, c.bmpand also dir/e.bmp.
*.bmp直接传递给find. find将递归遍历当前目录 ( .) 及其所有子目录(仅在示例中dir),并在这些目录中找到与模式匹配的所有文件。结果将是:b.bmp,c.bmp还有dir/e.bmp。
回答by cd1
The first command:
第一条命令:
find . -type f -name \*.bmp
passes an asterisk to the findcommand, and that tells it to find all the files in and below the current directory ending with .bmp.
将星号传递给find命令,并告诉它查找当前目录中和以 .bmp 结尾的所有文件。
The second command:
第二条命令:
find . -type f -name *.bmp
may be resolved by the shell to, for example:
外壳可能会解析为,例如:
find . -type f -name image1.bmp image2.bmp image3.bmp
(that would be the bmp files in the current directory only)
(那只会是当前目录中的 bmp 文件)
and findwould only list them, not the bmp files in other directories below the current one.
并且find只会列出它们,而不是当前目录下其他目录中的 bmp 文件。

