How can I add a string to the beginning of each file in a folder in bash?

前端 未结 9 895
天命终不由人
天命终不由人 2021-01-01 17:04

I want to be able to prepend a string to the beginning of each text file in a folder. How can I do this using bash on Linux?

9条回答
  •  醉酒成梦
    2021-01-01 17:28

    This should do the trick.

    FOLDER='path/to/your/folder'
    TEXT='Text to prepend'
    cd $FOLDER
    for i in `ls -1 $FOLDER`; do
         CONTENTS=`cat $i`
         echo $TEXT > $i  # use echo -n if you want the append to be on the same line
         echo $CONTENTS >> $i
    done
    

    I wouldn't recommending doing this if your files are very big though.

提交回复
热议问题