faz*_*oso 2 unix linux bash shell sh
我有一个文件列表,我想在文件的末尾添加一行.我找不到正确的方法:
find . | grep filexxx | xargs << echo "attribute=0000"
Run Code Online (Sandbox Code Playgroud)
不幸的是,似乎没有用.没有编写脚本,哪个oneliner命令会这样做?
谢谢!
您可以使用find的选项:-exec,如下所示:
find . -type f -name "file*" -exec bash -c 'echo "your line" >> $1' -- {} \;
Run Code Online (Sandbox Code Playgroud)
您需要更改文件*以匹配您要查找的文件.
还有另一种可能性:
find . -type f | while read file; do echo "your line" >> $file ; done
Run Code Online (Sandbox Code Playgroud)
您可以在上面找到grep或使用-name管道
编辑:
正如knittl在评论中所建议的那样,如果您的文件名包含新的行字符,您将遇到上述一个班轮的问题..并且由Gordon提供解决方案:
find . -type f -print0 | while IFS= read -r -d '' file; do ...
Run Code Online (Sandbox Code Playgroud)
小智 6
或者使用一个简单的循环:
for f in *txt; do echo "yada" >> "${f}"; done
Run Code Online (Sandbox Code Playgroud)