Printable Version of Topic

Click here to view this topic in its original format

Linuxhelp _ Programming in Linux _ Replacing text in all files of a directory

Posted by: JohnnyWraith May 28 2008, 04:12 PM

Hey all,

Thanks in advance to anyone who can help me out with this... Here's what I'm trying to do:

Basically I have a webpage (a pretty big one) with a bunch of html/jsp files. Recently, I was asked to change one of the three main directories' name. So instead of www.xxx.com/name1 now everything's gonna be www.xxx.com/name2. The problem here's that every link inside my files is gonna be wrong; Instead of linking to /name2 it's gonna link to /name1. Since there are a lot of files I was wondering if there was a way I could do this automatically.

There are a couple of extra things that have to be kept in mind... For instance, these html files might contain the word name1 (i.e. in a paragraph) and I don't want to do any replacing there. I think it should find instances of /name1* and replace them... I think we'd be fine with that...

Am I way over my head or is this possible?

Posted by: michaelk May 28 2008, 08:20 PM

Very easy.

http://www.brunolinux.com/02-The_Terminal/Find_and%20Replace_with_Sed.html

In your favorite text editor.

#!/bin/bash
for i in $( ls *.html); do
sed 'find/replace string goes here' $i
done

chmod 744 filename (to be executable only by root.)
To execute file:
./filename

Use \/name1 in order to use the / in your search string.

BTW typically for most linux distributions you must be root to edit files in the web server's directories.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)