27

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.

Jeff Atwood
  • 63,320
  • 48
  • 150
  • 153
Steve
  • 14,401
  • 35
  • 125
  • 230

5 Answers5

36

Use append redirection.

for f in *.txt
do
  cat footer >> "$f"
done
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
7

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
YOU
  • 120,166
  • 34
  • 186
  • 219
GreenMatt
  • 18,244
  • 7
  • 53
  • 79
1
sed -i.bak "$ a $(<file_block_of_text)" *.txt
kurumi
  • 25,121
  • 5
  • 44
  • 52
0

Variant of kurumi's answer:

sed -i.bak "\$aTEXTTOINSERT" *.txt

For more details, see SED: insert something to the last line?

Community
  • 1
  • 1
Joe Corneli
  • 642
  • 1
  • 6
  • 18
-1

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
Prabhu
  • 86
  • 6