命令查找目录中的所有文件并将它们连接起来作为参数?

发布于 2024-09-24 19:58:15 字数 253 浏览 0 评论 0原文

我正在寻找一个命令,它使用特定的模式查找目录中的所有文件,比如说“*.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 技术交流群。

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

发布评论

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

评论(6

憧憬巴黎街头的黎明 2024-10-01 19:58:15

如果您想对与通配符匹配的文件运行命令,则不需要额外的包袱:

mycommand *.txt

您记住的可能是 xargs 命令。它在其标准输入上获取文件名列表,并对这些文件运行命令。例如echo *.txt | xargs mycommand 是一种复杂且不可靠mycommand *.txt 编写方式。当 mycommand 必须操作的文件列表是其他命令的输出时,xargs 非常有用。

我说 xargs 不可靠的原因是它希望以一种特殊的方式引用它的输入:所有空格(不仅仅是换行符)单独的名称,反斜杠必须加倍,并且 ' 和 " 分隔文字字符串(其中只有反斜杠和末尾引号是特殊的)。由于很少有命令以 xargs 输入格式生成输出,这限制了 xargs 对于已知文件名不包含任何特殊字符的极少数情况很有用

find 命令通常与 xargs 结合使用。 code>,但这应该而且可以避免,而不是 find ... | xargs mycommand

find ... -exec mycommand {} +

If you want to run a command on files matched by a wildcard, you don't need extra baggage:

mycommand *.txt

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 example echo *.txt | xargs mycommand is a complicated, and unreliable, way of writing mycommand *.txt. xargs is useful when the list of files that mycommand 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 the xargs input format, this limits xargs'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 with xargs, but this should, and can, be avoided. Instead of find ... | xargs mycommand, write

find ... -exec mycommand {} +
給妳壹絲溫柔 2024-10-01 19:58:15

听起来你想要 lsxargs

It sounds like you want ls and xargs.

蒲公英的约定 2024-10-01 19:58:15

简单的答案是

mylist=$(ls *.txt)

注意:如果没有匹配项,ls 会报告错误。错误可以重定向到/dev/null。

对于 ls 和 find 以外的命令,如果没有匹配项,则使用通配符通常会返回通配符作为结果;所以需要处理不匹配的特殊情况。

以下将变量“list”设置为所有 .txt 文件,如果不存在此类文件,则将其设置为空字符串。相对于 ls(1) 的优点是 if 不调用外部命令。

list=$(echo *.txt)
if [ "$list" = "*.txt" ] ; then list=""; fi

运行我的命令的示例用法,但仅当列表不为空时:

test -n "$list" && my-command $list

现在,如果您的文件名称中可能有空格,那么数组更好:

List=()
let n=0
for x in *.txt
do
    test -e "$x" || exit
    List[n++]="$x"
done

然后将列表与

test ${#List[@]} -gt 0 && my-command "${List[@]}"

或 一起

for value in "$List[@]" ; do my-command "$value" ; done 

使用所有这些对于 ksh 来说可能有点不同,但是这将非常相似。

The simple answer is

mylist=$(ls *.txt)

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.

list=$(echo *.txt)
if [ "$list" = "*.txt" ] ; then list=""; fi

Example usage to run my command, but only if list is not empty:

test -n "$list" && my-command $list

Now, if your files may have spaces in their names, then an array is better:

List=()
let n=0
for x in *.txt
do
    test -e "$x" || exit
    List[n++]="$x"
done

Then use the list with

test ${#List[@]} -gt 0 && my-command "${List[@]}"

or

for value in "$List[@]" ; do my-command "$value" ; done 

All this may be a little different for ksh, yet it would be very similar.

滴情不沾 2024-10-01 19:58:15

您需要任何 Unix shell 的最基本功能之一。由于某种原因,它被称为“globbing”,并且它是内置的。

$ echo *.txt

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.

$ echo *.txt
戏剧牡丹亭 2024-10-01 19:58:15
$ echo "*.txt"
1.txt 2.txt ...
$ echo "*.txt"
1.txt 2.txt ...
冬天旳寂寞 2024-10-01 19:58:15

如果您只是在一个目录中查找文件,而不是在嵌套目录中查找文件,则可以使用 glob 命令的参数,该参数将由 shell 解释,并为每个匹配的文件名生成一个参数给命令:

$ echo *.txt
file1.txt file2.txt file3.txt

如果您正在查找子目录中任意深度嵌套的文件,find 确实是您要查找的内容。您需要传入您正在查找的目录,并使用 -name 参数和带引号的 glob 表达式来查找相应的文件(如果 glob 未带引号,shell 将尝试解释它,并替换当前目录中与该名称匹配的文件列表,这不是您想要的):

$ find . -name "*.txt"
file1.txt. file2.txt file3.txt subdir/file.txt

如果您想将这些作为参数传递给另一个函数,您可以使用 $() (这相当于更熟悉的反引号``),

echo $(find . -name "*.txt")

或者如果你要得到一个对于单个参数列表来说太长的列表,或者需要支持文件名中的空格,你可以使用xargs,它将输入分成块,每个块它足够小,可以传递到命令中。

find . -name "*.txt" | xargs echo

如果您的文件名包含空格,那么 xargs 会将它们分成两部分,将每个部分视为一个单独的参数,这不是您想要的。您可以通过使用空字符 0 作为分隔符以及 find-print0 参数和 - 来避免这种情况。 xargs 的 0 参数。

find . -name "*.txt" -print0 | xargs -0 echo

If 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:

$ echo *.txt
file1.txt file2.txt file3.txt

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):

$ find . -name "*.txt"
file1.txt. file2.txt file3.txt subdir/file.txt

If you want to pass these in as arguments to another function, you can use $() (which is equivalent to the more familiar backquotes ``),

echo $(find . -name "*.txt")

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.

find . -name "*.txt" | xargs echo

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 to find and the -0 argument to xargs.

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