Shell script to append text to each file?

后端 未结 5 880
抹茶落季
抹茶落季 2020-12-10 01:16

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 co

相关标签:
5条回答
  • 2020-12-10 01:50

    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
    
    0 讨论(0)
  • 2020-12-10 01:57

    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
    
    0 讨论(0)
  • 2020-12-10 01:59

    Use append redirection.

    for f in *.txt
    do
      cat footer >> "$f"
    done
    
    0 讨论(0)
  • 2020-12-10 01:59

    Variant of kurumi's answer:

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

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

    0 讨论(0)
  • 2020-12-10 02:02
    sed -i.bak "$ a $(<file_block_of_text)" *.txt
    
    0 讨论(0)
提交回复
热议问题