Linux find 和 ls 上的通配符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13423824/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:48:44  来源:igfitidea点击:

wild cards on find and ls

linuxbash

提问by Juan Pablo Barrios

I'm trying to figure out the wild-cards to do file operations.

我试图找出通配符来进行文件操作。

I have these files in a directory for testing purposes:

我将这些文件放在一个目录中以进行测试:

file_BSD.GIF  file_linux.gif  file_unix

See my ls command,

看我的 ls 命令,

$ ls *{.GIF,.gif}
file_BSD.GIF  file_linux.gif

Which is OK.

这是好的。

But "find" doesn't seem to work the same way:

但是“查找”的工作方式似乎不同:

$ find -name *{.GIF,.gif}
find: paths must precede expression: file_linux.gif
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

By the way, I've read that "-iname" should locate both the uppercase and lowercase files, but that doesn't seem to work either:

顺便说一句,我读过“-iname”应该同时定位大写和小写文件,但这似乎也不起作用:

$find -iname *.gif

./file_linux.gif

(This should locate the .GIF file as well, right?).

(这也应该找到 .GIF 文件,对吗?)。

采纳答案by Shiplu Mokaddim

find -name *{.GIF,.gif}is wrong.

find -name *{.GIF,.gif}是错的。

This command is first exapanded by shell to find -name *.GIF *.gif

这个命令首先被shell扩展为 find -name *.GIF *.gif

Then farther expanded to

然后进一步扩展为

find -name file_BSD.GIF  file_linux.gif 
# as you have only these files in directory

Now this -name file_BSD.GIF file_linux.gifis passed to find. And this is wrongas there is no switch like file_linux.gifthat is accepted by find.

现在这-name file_BSD.GIF file_linux.gif被传递给find. 这是错误的,因为file_linux.giffind没有接受这样的开关。

What you need is this command.

你需要的是这个命令。

find -name '*.GIF' -or -name '*.gif'

Which is same as

这与

find -iname '*.gif'

Note the single quotes (') here. It means *.GIFshould be sent to find as iswithout any shell expansion. And find will use this as pattern. This single quote is necessary unless you escape the shell meta-characters. In that case the command would look like

请注意'此处的单引号 ( )。这意味着*.GIF应按原样发送查找没有任何外壳扩展。find 将使用它作为模式。除非您转义 shell 元字符,否则此单引号是必需的。在这种情况下,命令看起来像

find -iname \*.gif

回答by Nicolás Ozimica

You are having trouble with the parameter -inameof findbecause you must quote the patterns you give to it.

您遇到了参数-inameof 的问题,find因为您必须引用您提供给它的模式。

So, you should do:

所以,你应该这样做:

find -iname '*.gif'

This is stated in the manual:

这在手册中有说明:

"... Please note that you should quote patterns as a matter of course, otherwise the shell will expand any wildcard characters in them."

“...请注意,您理所当然地应该引用模式,否则 shell 将扩展其中的任何通配符。”

回答by Basile Starynkevitch

You should understand that (in contrast to Windows) the shell is expanding the *{.GIF,.gif}before passing it to the findprogram.

您应该了解(与 Windows 相比)shell*{.GIF,.gif}在将其传递给find程序之前会对其进行扩展。

You can feel what the shell does by replacing the program with echo.

您可以通过将程序替换为echo.

So you should quote the program argument, like

所以你应该引用程序参数,比如

   echo \-name '*{.GIF,.gif}'

so run

所以跑

   find -name '*.{GIF,gif}'

Maybe you want

也许你想要

   find -name '*.gif' -o -name '*.GIF'

Please read the Advanced Bash Scripting Guide(and perhaps the execve(2)man page, to understand how the kernel run programs).

请阅读Advanced Bash Scripting Guide(也许还有execve(2)手册页,以了解内核如何运行程序)。