[英]Learning bash: Append a line to list of files
I have a list of files of which I want to add a line to the end of the file. 我有一个文件列表,我想在文件的末尾添加一行。 I cannot find the correct way to do it:
我找不到正确的方法:
find . | grep filexxx | xargs << echo "attribute=0000"
Does not seem to work, unfortunately. 不幸的是,似乎没有用。 Without writing a script, which oneliner command would do it?
没有编写脚本,哪个oneliner命令会这样做?
thanks! 谢谢!
You can use find's option: -exec like this: 您可以使用find的选项:-exec,如下所示:
find . -type f -name "file*" -exec bash -c 'echo "your line" >> $1' -- {} \;
you need to change file* to match files you are looking for. 您需要更改文件*以匹配您要查找的文件。
There is also another possibility: 还有另一种可能性:
find . -type f | while read file; do echo "your line" >> $file ; done
you can pipe find to grep or use -name in the above 您可以在上面找到grep或使用-name管道
edit: 编辑:
as suggested by knittl in comments, you would have problems with the above one liner if your filename contains new line character.. and solution provided by Gordon : 正如knittl在评论中所建议的那样 ,如果您的文件名包含新的行字符,您将遇到上述一个班轮的问题..并且由Gordon提供解决方案:
find . -type f -print0 | while IFS= read -r -d '' file; do ...
或者使用一个简单的循环:
for f in *txt; do echo "yada" >> "${f}"; done
There's a lot of ways to do this. 有很多方法可以做到这一点。 wisent's answer is one.
明智的回答是一个。 Here is another, perhaps more intuitive, even if it spawns more processes:
这是另一个,也许更直观,即使它产生更多的过程:
for fname in $(find . | grep filexxx) ; do echo "attribute=0000" >> $fname ; done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.