Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a bunch of HTML files, and I need to add <script src="ga.js"></script> before each search instance of "</body></html>".

How would I add that to the bottom of all these files?

share|improve this question
up vote 9 down vote accepted

If they're actually on the same line, you can do it pretty trivially with sed:

$ sed -i '/<\/body><\/html>/ i <script src="ga.js"></script>' *.html

That finds all html files in the current directory (*.html), searches for </body></html> (/.../), and inserts (i) the script tag before it, writing the changes back to the same file (-i)

share|improve this answer
You can probably drop the search for <\/html>. Even if it turns up on another line, the important part is that this ends up before the closing body tag. – Caleb Jul 8 '11 at 6:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.