命令查找目录中的所有文件并将它们连接起来作为参数?
我正在寻找一个命令,它使用特定的模式查找目录中的所有文件,比如说“*.txt”,并在 BASH 中创建一个参数列表。
因此,如果目录包含: 文件1.txt 文件2.txt 文件3.txt nonsense.c
我需要字符串“file1.txt file2.txt file3.txt”
我知道有一个 BASH/Unix 命令用于此目的,但我不记得了:-S。 “查找”对我不起作用......
谢谢!
伊万·詹森斯
I'm looking for a command which finds all files in a directory using a specific partern, lets say "*.txt" and create a list of parameters from it in BASH.
So if the dir contains:
file1.txt
file2.txt
file3.txt
nonsense.c
I need the string "file1.txt file2.txt file3.txt"
I knew there was a BASH/Unix command for this, but I can't remember it :-S. "find" didn't work for me...
Thanks!
Yvan Janssens
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您想对与通配符匹配的文件运行命令,则不需要额外的包袱:
您记住的可能是 xargs 命令。它在其标准输入上获取文件名列表,并对这些文件运行命令。例如
echo *.txt | xargs mycommand
是一种复杂且不可靠的mycommand *.txt
编写方式。当mycommand
必须操作的文件列表是其他命令的输出时,xargs
非常有用。我说 xargs 不可靠的原因是它希望以一种特殊的方式引用它的输入:所有空格(不仅仅是换行符)单独的名称,反斜杠必须加倍,并且
' 和
"
分隔文字字符串(其中只有反斜杠和末尾引号是特殊的)。由于很少有命令以xargs
输入格式生成输出,这限制了xargs 对于已知文件名不包含任何特殊字符的极少数情况很有用
find
命令通常与xargs
结合使用。 code>,但这应该而且可以避免,而不是。find ... | xargs mycommand
If you want to run a command on files matched by a wildcard, you don't need extra baggage:
What you remember is probably the
xargs
command. It takes a list of file names on its standard input, and runs a command on these files. For exampleecho *.txt | xargs mycommand
is a complicated, and unreliable, way of writingmycommand *.txt
.xargs
is useful when the list of files thatmycommand
must act on is the output of some other command.The reason I said
xargs
is unreliable is that it expects its input to be quoted in a peculiar way: all whitespace (not just newlines) separate names, backslashes must be doubled, and'
and"
delimit literal strings (in which only backslash and the end quote are special). Since few commands produce output in thexargs
input format, this limitsxargs
's usefulness to those rare cases where it is known that file names will not contain any of the special characters.The
find
command is often used in combination withxargs
, but this should, and can, be avoided. Instead of, writefind ... | xargs mycommand
听起来你想要
ls
和xargs
。It sounds like you want
ls
andxargs
.简单的答案是
注意:如果没有匹配项,ls 会报告错误。错误可以重定向到/dev/null。
对于 ls 和 find 以外的命令,如果没有匹配项,则使用通配符通常会返回通配符作为结果;所以需要处理不匹配的特殊情况。
以下将变量“list”设置为所有 .txt 文件,如果不存在此类文件,则将其设置为空字符串。相对于 ls(1) 的优点是 if 不调用外部命令。
运行我的命令的示例用法,但仅当列表不为空时:
现在,如果您的文件名称中可能有空格,那么数组更好:
然后将列表与
或 一起
使用所有这些对于 ksh 来说可能有点不同,但是这将非常相似。
The simple answer is
NOTE: ls reports an error if nothing matches. Errors can be redirected to /dev/null.
For commands other than ls and find, if there are no matches, then wildcard usage usually returns the wildcard as the result; so it is necessary to handle the special case of no matches.
The following sets the variable "list" to all .txt files, or sets it to the empty string if no such files exist. The advantage over ls(1) is that if invokes no external command.
Example usage to run my command, but only if list is not empty:
Now, if your files may have spaces in their names, then an array is better:
Then use the list with
or
All this may be a little different for ksh, yet it would be very similar.
您需要任何 Unix shell 的最基本功能之一。由于某种原因,它被称为“globbing”,并且它是内置的。
You have asked for one of the most basic features of any Unix shell. It's known as "globbing", for some reason, and it's built-in.
如果您只是在一个目录中查找文件,而不是在嵌套目录中查找文件,则可以使用 glob 命令的参数,该参数将由 shell 解释,并为每个匹配的文件名生成一个参数给命令:
如果您正在查找子目录中任意深度嵌套的文件,
find
确实是您要查找的内容。您需要传入您正在查找的目录,并使用-name
参数和带引号的 glob 表达式来查找相应的文件(如果 glob 未带引号,shell 将尝试解释它,并替换当前目录中与该名称匹配的文件列表,这不是您想要的):如果您想将这些作为参数传递给另一个函数,您可以使用
$()
(这相当于更熟悉的反引号``
),或者如果你要得到一个对于单个参数列表来说太长的列表,或者需要支持文件名中的空格,你可以使用
xargs
,它将输入分成块,每个块它足够小,可以传递到命令中。如果您的文件名包含空格,那么 xargs 会将它们分成两部分,将每个部分视为一个单独的参数,这不是您想要的。您可以通过使用空字符
0
作为分隔符以及find
的-print0
参数和- 来避免这种情况。
参数。xargs
的 0If you are just looking for files in one directory, not in nested directories, you can use a glob argument to your command, which will be interpreted by the shell and produce one argument to your command for each matching filename:
If you are looking for files nested arbitrarily deeply within subdirectories,
find
is indeed what you are looking for. You need to pass in the directory you are looking in, and use the-name
parameter with a quoted glob expression to find the appropriate files (if the glob is not quoted, the shell will try to interpret it, and substitute a list of files matching that name in the current directory, which is not what you want):If you want to pass these in as arguments to another function, you can use
$()
(which is equivalent to the more familiar backquotes``
),Or if you are going to get a list that is too long for a single argument list, or need to support spaces in your filename, you can use
xargs
, which divides its input up into chunks, each of which is small enough to be passed into a command.If your filenames contain whitespace, then
xargs
will split them up into two pieces, treating each as a separate argument, which is not what you want. You can avoid this by using the null character,0
, as the delimiter, with the-print0
argument tofind
and the-0
argument toxargs
.