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

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


This will do that. You could make it more efficient if you are doing the same text to each file...

for f in *; do   echo "whatever" > tmpfile  cat $f >> tmpfile  mv tmpfile $fdone


You can do it like this without a loop and cat

sed -i '1i whatever' *

if you want to back up your files, use -i.bak

Or using awk

awk 'FNR==1{$0="whatever\n"$0;}{print $0>FILENAME}' *


And you can do this using sed in 1 single command as well

for f in *; do  sed -i.bak '1i\  foo-bar  ' ${f}done