[英]Xargs append string to files
I have multiple text files in a directory.我在一个目录中有多个文本文件。 At the end I want to append a string.
最后我想要 append 一个字符串。
Eg.例如。 List of text files in directory.
目录中的文本文件列表。
Command to get their path:命令获取他们的路径:
find -name "*txt"
Then I tried to send然后我试着发送
'echo "example text" > <filename>
So I tried running the following:所以我尝试运行以下命令:
find -name "*txt" | xargs echo "example_text" >>
This command fails.此命令失败。
All I want to is append some text to the files every now and then and since the names of the files keep changing I wanted to use xargs我想要的只是 append 时不时地向文件添加一些文本,因为文件的名称不断变化,我想使用 xargs
xargs
isn't really appropriate here. xargs
在这里并不合适。 Maybe a loop over filenames like也许循环遍历文件名
for file in *.txt; do
echo "example_text" >>"$file"
done
Because >>
is a shell directive, if you want it honored via xargs, you need to have xargs start a shell. As Shawn's answer demonstrates, in many cases a shell glob is enough and you don't need find
at all;因为
>>
是一个 shell 指令,如果你想通过 xargs 兑现它,你需要让 xargs 启动一个 shell。正如Shawn 的回答所示,在许多情况下,一个 shell glob 就足够了,你根本不需要find
; but if you do want to use find
, it can be used correctly either with or without xargs.但是如果你确实想使用
find
,无论有没有 xargs 都可以正确使用它。
If you insist on using xargs
, even though it isn't the best tool for the job...如果您坚持使用
xargs
,即使它不是完成这项工作的最佳工具......
find . -name "*.txt" -print0 | xargs -0 sh -c '
for arg in "$@"; do echo "example_text" >>"$arg"; done
' _
Taking xargs
out, and just using find
(with -exec... {} +
to get the same performance benefits xargs
would otherwise offer):取出
xargs
,仅使用find
(使用-exec... {} +
获得xargs
否则会提供的相同性能优势):
find . -name "*.txt" -exec sh -c '
for arg in "$@"; do echo "example_text" >>"$arg"; done
' _ {} +
(in both of the above, the _
substitutes for $0
, so later arguments become $1
and later, and are thus iterated over when expanding "$@"
). (在上面两个中,
_
替代$0
,所以后来 arguments 变成$1
及以后,因此在扩展"$@"
时被迭代)。
tee -a
! tee -a
! Un*x command tee
are built for this kind of operation and is a lot quicker!! Un*x 命令
tee
是为这种操作而构建的,而且速度要快得多!!
find . -type f -name '*.txt' -exec tee -a <<<'Foo bar baz' {} >/dev/null +
But herestring will work only if tee
are executed only once!但是只有当
tee
只执行一次时, herestring才会起作用! (thanks to Charles Duffy's comment )! (感谢Charles Duffy 的评论)!
See pure bash using globstar
further.使用
globstar
进一步查看纯bash 。
xargs
xargs
find . -type f -name '*.txt' -print0 |
xargs -0 sh -c 'echo "Foo bar baz"|tee -a "$@" >/dev/null ' _
find
really required?find
真的需要吗? If all files are under same directory:如果所有文件都在同一目录下:
tee -a <<<'Foo bar baz' >/dev/null *.txt
Else, under [ŧag:bash], using globstar ( shopt -s globstar
):否则,在 [ŧag:bash] 下,使用globstar (
shopt -s globstar
):
tee -a <<<'Foo bar baz' >/dev/null **/*.txt
As many pointed out, xargs
is not appropriate, so you could just simply use find and pipe to read
in a loop to accomplish what you want easily as shown below.正如许多人指出的那样,
xargs
不合适,因此您可以简单地使用 find 和 pipe 循环read
以轻松完成您想要的操作,如下所示。
find . -name "*.txt" | while read fname; do echo "example_text">>$fname; done
From the point of view of bash
, there are three parts in your command:从
bash
的角度来看,您的命令分为三个部分:
find -name "*txt"
)find -name "*txt"
)xargs echo "example_text"
)xargs echo "example_text"
)
) bash
tries to open the output file provided after the redirection but, as you didn't provided anything, bash
cannot open "nothing" and fails. bash
尝试打开重定向后提供的 output 文件,但是由于您没有提供任何内容, bash
无法打开“无”并失败。
To solve your issue, you need to give xargs
a way to add the line you need to the file (without asking bash
to redirect the output of xargs
).要解决您的问题,您需要为
xargs
提供一种将所需行添加到文件的方法(无需要求bash
重定向xargs
的 output)。 A way that should work could be by starting a subshell that performs that operation:一种可行的方法是启动一个执行该操作的子 shell:
find -name "*txt" | xargs -I{} bash -c 'echo "example_text" >> {}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.