1

Possible Duplicate:
Find and Replace Inside a Text File from a Bash Command

I've been told to change all the title tags in a website.

Because the title tag it's in all the pages of the website (more than 30) and it's static, I need to go file by file in order to replace this title with the new one.

So I was wondering if someone knows a script that can achieve this without having to do it manually in a one by one basis.

Example:

/var/www/websitename/

Inside this path, there is multiple .html files which I have to change the title from:

 <title>Old title</title>

To:

 <title>New title</title>
Community
  • 1
  • 1
rfc1484
  • 9,441
  • 16
  • 72
  • 123

4 Answers4

3

Try this one (after a backup of the folder, because here sed works in-place).

find /var/www/websitename/ -name '*.html' -exec sed -i.bak 's/.title.Old title..title./<title>New title<\/title>/g' {} \;
Avio
  • 2,700
  • 6
  • 30
  • 50
2
perl -pi -e 's/\<title\>Old title\<\/title\>/\<title\>New title\<\/title\>/g' *.html
Vijay
  • 65,327
  • 90
  • 227
  • 319
0

You can use notepad++ as well. Open all files (just mark them and drag on notepad++ or drag the whole folder), press ctrl+f, switch to second tab("replace" or something like this - i'm not using english version), type your text(you can use regexp as well - see options) and click "replace in all opened files"(or someting like this). It's not as powewrfull solution as using scripts, but it's easier and often it's enough.

cyriel
  • 3,522
  • 17
  • 34
0

Try this:

#!/bin/bash

old_title="<title>Old title<\/title>"
new_title="<title>New title<\/title>"

for file in $(find . -name "*.html");
do
    `sed -i "s/${old_title}/${new_title}/g" ${file}`
done
tito
  • 11
  • 4