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?
Asked
Active
Viewed 3,800 times
3
-
Is [this](http://stackoverflow.com/questions/6329505/how-to-rename-all-file-in-a-folder-with-a-suffix-in-a-single-unix-command) what you're looking for? – Alex Kalicki Apr 28 '13 at 18:34
-
@Snowsickle No, I don't need to change the name, just the contents. – user2329778 Apr 28 '13 at 18:36
-
Are there any other files in that folder? – Beta Apr 28 '13 at 18:38
-
@Beta Nothing other than the text files that I need to edit. – user2329778 Apr 28 '13 at 18:39
-
Related (even duplicate): http://askubuntu.com/questions/11031/add-a-string-to-a-text-file-from-terminal – Ionică Bizău Apr 28 '13 at 18:39
2 Answers
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
-
-
-
Tested on Linux not Mac, edited to make Mac friendly. @GoZoner no, `-i`. – Chris Seymour Apr 28 '13 at 18:48
-
1@sudo_O Anyway to get the zeros on the same line as the text? Sorry, in a bit over my head here :) – user2329778 Apr 28 '13 at 19:01
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