Adding text to the beginning and end of a number of files?

Posted by John Feminella on Super User See other posts from Super User or by John Feminella
Published on 2012-08-30T14:53:46Z Indexed on 2012/08/30 15:40 UTC
Read the original article Hit count: 234

Filed under:
|
|

I have a number of files in a directory hierarchy. For each file, I'd like to add "abcdef" to the beginning, on its own line, and "ghijkl" to the end, on its own line. For example, if the files initially contained:

# one/foo.txt
apples
bananas

# two/three/bar.txt
coconuts

Then afterwards, I'd expect them to contain:

# one/foo.txt
abcdef
apples
bananas
ghijkl

# two/three/bar.txt
abcdef
coconuts
ghijkl

What's the best way to do this?

I've gotten as far as:

# put stuff at start of file
find . -type f -print0 | xargs -0 sed -i 's/.../abcdef/g'

# put stuff at end of file
find . -type f -print0 | xargs -0 sed -i 's/.../ghijkl/g'

but I can't seem to figure out how what to put in the ellipses.

© Super User or respective owner

Related posts about text

Related posts about sed