1

I am looking for bash code to replace all <title>XXX</title> within all .html files to <title>XXX - YYY $Date</title>

What I came up with, using sed is:

sed -i "s/</title>/ - YYY 'date'/g" /home/mirror/*

This however, doesn't implement the .html requirement and I can't get it to work, I'm either getting

sed: no imput files

or

-bash: s/</title>/ - YYY 'date'/g: No such file or directory

The structure is:

/home/mirror/
├── images
├── archive
│   └── index.html
│   └── 2.html
├── index.html
├── 2.html
├── style.css
└── scripts.js

How can I do this?

jackar
  • 99
  • 1
  • 1
  • 10
  • see also 1) https://stackoverflow.com/questions/5864146/use-slashes-in-sed-replace 2) https://stackoverflow.com/questions/7680504/sed-substitution-with-bash-variables 3) https://stackoverflow.com/questions/1583219/awk-sed-how-to-do-a-recursive-find-replace-of-a-string/1585810 – Sundeep Dec 16 '17 at 11:26

1 Answers1

3

You need find to recurse through the directories:

find /home/mirror -name '*.html' -exec sed -i "s@</title>@ - YYY $Date</title>@g" {} +

Note I replaced "'date'" with "$Date" as that's what you said you wanted to add. I also use @ instead of / as the sed s separator character, to avoid interference from the </title> parts.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 2
    thank you, this worked exactly as I needed & I learned a little bit more today. have a nice day John Zwinck :) – jackar Dec 16 '17 at 11:25