简体   繁体   English

Xargs append 字符串到文件

[英]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.目录中的文本文件列表。

  • a.txt一个.txt
  • b.txt b.txt
  • c.txt c.txt

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及以后,因此在扩展"$@"时被迭代)。

Append a string to multiple files, using tee -a ! Append 一个字符串到多个文件,使用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 using globstar further.使用globstar进一步查看

And if really you want use xargs如果你真的想使用xargs

find . -type f -name '*.txt' -print0 |
    xargs -0 sh -c 'echo "Foo bar baz"|tee -a "$@" >/dev/null ' _

But are 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的角度来看,您的命令分为三个部分:

  • Before the pipe character ( find -name "*txt" )在 pipe 字符之前( find -name "*txt"
  • Between the pipe and the redirection ( xargs echo "example_text" )在 pipe 和重定向之间( xargs echo "example_text"
  • After the redirection (重定向后( ) )

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM