Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepend to multiple files in bash/osx terminal

Tags:

shell

I would like to prepend some text to multiple files in bash, I've found this post that deals with prepend: prepend to a file one liner shell?

And I can find all the files I need to process using find:

find ./ -name "somename.txt"

But how do I combine the two using a pipe?

like image 783
DEfusion Avatar asked Feb 06 '09 15:02

DEfusion


1 Answers

You've got several options. Easiest is probably sed:

find ./ -name somename.txt -exec sed -e '1i\
My new text here' {} \;

It'll be faster if you add '2q' to tell it you're done after prepanding the text, and if will happen in place in the file with the -i flag:

find ./ -name somename.txt -exec sed -i .bak -e '2q;1i\
My new text here' {} \;

To prepend multiple lines, you'll need to end each one with a backslash.

That leaves the original files around with a .bak extension.

like image 116
Charlie Martin Avatar answered Nov 03 '22 09:11

Charlie Martin