2
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.
I can’t help but notice how simple and straightforward this is in Windows:
for %i in (*) do echo abcdef>t.tmp & type "%i">>t.tmp & echo ghijkl>>t.tmp & move /y t.tmp "%i"
or alternately,echo abcdef>header.tmp & echo ghijkl>footer.tmp & for %i in (*) do copy /y headert.tmp+"%i"+footer.tmp "%i" & del header.tmp & del footer.tmp
– Synetech – 2012-08-30T15:27:23.313@Synetech: Does that work recursively? – Dennis – 2012-08-30T15:37:01.780
1@Dennis, as in for files in subfolders? No, but you can add a
/r
to thefor
loop to make it recursive. – Synetech – 2012-08-30T16:41:11.817