bash shell 中的 find 命令和 -name 选项疑问
下面两者有什么区别:
find . -type f -name \*.bmp
find . -type f -name *.bmp
我已经测试过,它们都返回相同的结果,那么有什么不同_deep inside_
吗?
从删除的答案中添加:
所以这是为了避免特殊*****字符的shell扩展,只需通过* 作为 find 命令的参数并让它处理它。
但在我的机器上,它们都很好,都返回当前目录及其下的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
系统信息:
GNU bash,版本4.1.5(1 )-release (i486-pc-linux-gnu)
Linux sysabod-laptop 2.6.32-30-generic #59-Ubuntu SMP 3 月 1 日星期二 21:30:21 UTC 2011 i686 GNU/Linux
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_
?
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.
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
./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)
Linux sysabod-laptop 2.6.32-30-generic #59-Ubuntu SMP Tue Mar 1 21:30:21 UTC 2011 i686 GNU/Linux
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它们的不同之处如下:第一个总是有效,而第二个则无效。
至于为什么:在 bash 中,shell glob(通配符模式,包括 * 或 ?)被 shell 扩展为与 glob 匹配的所有文件。但是,如果不存在此类文件,则该模式将保持不变。
因此,如果您所在的目录没有
bmp
文件,这些命令的工作方式是相同的,因为第一个被转义,并且 bash 无法找到与第二种情况匹配的任何文件。如果您从仅包含一个此类文件的目录运行它,例如
foo.bmp
,第一个将找到子树中的所有bmp
文件,而第二个将找到所有文件仅命名为foo.bmp
。如果在包含多个 bmp 文件的目录中运行,我相信您会收到错误,因为 find 不知道如何处理所有文件名。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.
So, if you're in a directory with no
bmp
files, the commands work the same way, because the first is escaped and bash fails to find any files matching in the second case.If you ran it from a directory containing only one such file, say
foo.bmp
, the first would find allbmp
files in the subtree, while the second would find all files namedfoo.bmp
only. If run in a directory with multiplebmp
files, I believe you'll get an error becausefind
doesn't know what to do with all the filenames.当您转义星号 (
\*
) 时,星号本身将作为参数传递给find
命令,并由find
进行计算。如果您没有转义星号 (*
),shell 就会对其进行求值并将其扩展为与模式匹配的文件名。例如,考虑以下目录结构:
当您执行
shell 时,会将
*.bmp
扩展为b.bmp c.bmp
。即实际执行的命令将是:它将找到
b.bmp
和c.bmp
,但不会找到dir/e.bmp
。当您执行时,
*.bmp
会直接传递给find
。find
将递归当前目录 (.
) 及其所有子目录(在示例中仅dir
),并查找这些目录中的所有文件匹配模式。结果将是:b.bmp
、c.bmp
以及dir/e.bmp
。When you escape the asterisk (
\*
) the asterisk itself is passed as argument to thefind
command and will be evaluated byfind
. If you don't escape the asterisk (*
) already the shell evaluates it and expands it to the file names matching the pattern.Fore example consider following directory structure:
When you execute
the shell expands
*.bmp
tob.bmp c.bmp
. I.e. the command that is actually executed will be:which will find
b.bmp
andc.bmp
but notdir/e.bmp
.When you execute
*.bmp
is passed directly as it is tofind
.find
will recurse through the current directory (.
) and all its subdirectories (in the example onlydir
) and will find all files in those directories matching the pattern. The result will be:b.bmp
,c.bmp
and alsodir/e.bmp
.第一个命令:
向
find
命令传递一个星号,告诉它查找当前目录及其下以 .bmp 结尾的所有文件。第二个命令:
可以由 shell 解析为,例如:(
仅当前目录中的 bmp 文件)
并且
find
只会列出它们,而不是当前目录下的其他目录。The first command:
passes an asterisk to the
find
command, and that tells it to find all the files in and below the current directory ending with .bmp.The second command:
may be resolved by the shell to, for example:
(that would be the bmp files in the current directory only)
and
find
would only list them, not the bmp files in other directories below the current one.