bash shell 中的 find 命令和 -name 选项疑问

发布于 2024-11-01 02:57:01 字数 819 浏览 4 评论 0原文

下面两者有什么区别:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七月上 2024-11-08 02:57:01

它们的不同之处如下:第一个总是有效,而第二个则无效。

至于为什么:在 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 all bmp files in the subtree, while the second would find all files named foo.bmp only. If run in a directory with multiple bmp files, I believe you'll get an error because find doesn't know what to do with all the filenames.

深空失忆 2024-11-08 02:57:01

当您转义星号 (\*) 时,星号本身将作为参数传递给 find 命令,并由 find 进行计算。如果您没有转义星号 (*),shell 就会对其进行求值并将其扩展为与模式匹配的文件名。

例如,考虑以下目录结构:

./a.txt
./b.bmp
./c.bmp
./dir/d.doc
./dir/e.bmp

当您执行

find . -type f -name *.bmp

shell 时,会将 *.bmp 扩展为 b.bmp c.bmp。即实际执行的命令将是:

find . -type f -name b.bmp c.bmp

它将找到 b.bmpc.bmp,但不会找到 dir/e.bmp

当您执行时,

find . -type f -name \*.bmp

*.bmp 会直接传递给 findfind 将递归当前目录 (.) 及其所有子目录(在示例中仅 dir),并查找这些目录中的所有文件匹配模式。结果将是:b.bmpc.bmp 以及 dir/e.bmp

When you escape the asterisk (\*) the asterisk itself is passed as argument to the find command 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.

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 *.bmp to b.bmp c.bmp. I.e. the command that is actually executed will be:

find . -type f -name b.bmp c.bmp

which will find b.bmp and c.bmp but not dir/e.bmp.

When you execute

find . -type f -name \*.bmp

*.bmp is passed directly as it is to find. find will 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.bmp and also dir/e.bmp.

自我难过 2024-11-08 02:57:01

第一个命令:

find . -type f -name \*.bmp

find 命令传递一个星号,告诉它查找当前目录及其下以 .bmp 结尾的所有文件。

第二个命令:

find . -type f -name *.bmp

可以由 shell 解析为,例如:(

find . -type f -name image1.bmp image2.bmp image3.bmp

仅当前目录中的 bmp 文件)

并且 find 只会列出它们,而不是当前目录下的其他目录

The first command:

find . -type f -name \*.bmp

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:

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)

and find would only list them, not the bmp files in other directories below the current one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文