I have a bunch of html files and I want to cat them all (individually) with a header file (several lines at least in length), say header.txt. I'm almost there. I can cat each file with the header file with the following command, but how can I then output the result to save it under the original filename?
find . -iname "*.html" | xargs -L 1 cat header.txt
The above prints out what I want, but how can I then reference the xargs
argument to add a final > original file name?
So in short, I want to insert the text from header.txt into the beginning of every html file. Can I modify the above command to do so? Or alternately are there other ways to achieve this?
As an update, here's what I've tried since posting, which hasn't worked:
find . -iname "*.html" | xargs -L 1 cat header.txt {} > {}.new
This does fine except it prints to a file literally named {}.new
. That file looks as it should but this isn't helpful.
Also tried @Dave's suggestion, but there is a syntax error I can't fix:
find . -iname "*.html" -exec (cat header.txt; cat {}) > {}.new
-bash: syntax error near unexpected token `('
header.txt
at the beginning of all your*.html
files. Have I understood you correctly? – Chris Davies Oct 28 '15 at 18:15cat
ing a file and you're performing afind
. I would lean towards something likefind . -iname "*.html" -exec (cat header.txt; cat {}) > {}.new
I would also check over the new files before scripting them to overwrite the originals. (I didn't test that command, it was meant to give an idea) – Centimane Oct 28 '15 at 18:21awk
may also be a good tool for this, as you can insert text at line 1 using it – Centimane Oct 28 '15 at 18:28cat header file > file
throws an error, and so command grouping is required – Centimane Oct 28 '15 at 18:31rook
– Chris Davies Oct 28 '15 at 20:04