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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With