Shell script to append text to each file?
I have a folder full of text files. I need to append the same block of text to each of them (and of course overwrite the original file).
I was wondering what the correct Bash shell syntax would be for this开发者_如何学运维. Would I use cat?
I have done a few batch scripts but I'm not a Bash expert. Any suggestions appreciated.
Use append redirection.
for f in *.txt
do
cat footer >> "$f"
done
If you're needing to do this via a script, you can use echo and append redirection to get the extra text into the files.
FILES=pathto/*
for f in $FILES ; do
echo "#extra text" >> $f
done
sed -i.bak "$ a $(<file_block_of_text)" *.txt
Variant of kurumi's answer:
sed -i.bak "\$aTEXTTOINSERT" *.txt
For more details, see SED: insert something to the last line?
very simply one which worked well for me
#!/bin/sh
FILES="./files/*"
for f in $FILES
do
echo '0000000' | cat - $f > temp && mv temp $f
done
精彩评论