3

I have a folder with 1,000 small text files in it, and I need to modify the files and add 7 zeroes to the beginning of every one. After I do this I'll be able to cat them all together. Is there an easy way to do this in terminal?

user2329778
  • 51
  • 1
  • 3

2 Answers2

6

Find all files in the current directory and insert 0000000 at the beginning of the file using sed:

find . -maxdepth 1 -type f -exec sed -i.bk '1i \
0000000' {} \;

This will also create .bk file for each file in directory. If you are happy with the result just rm *.bk to delete the back up files.

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
4

Paste this in a .sh file and execute it. Put the path to your file directory instead of the example one.

#!/bin/bash
FILES="./files/*"

for f in $FILES
do
    echo '0000000' | cat - $f > temp && mv temp $f
done
Nicola Miotto
  • 3,647
  • 2
  • 29
  • 43