简体   繁体   中英

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?

thanks!

You can use find's option: -exec like this:

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

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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