Link to home
Start Free TrialLog in
Avatar of mBf
mBf

asked on

Replacing text with another text in a certain directory recursively using a bash script

Greetings,

I want a shell script that can replace a text with another text for special file types (*.html, for an instance) in a certain directory recursivle. The modified file has of course to be in the same location of the original file.

Thanks,
Hossam
ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mBf
mBf

ASKER

Works.
Suppose that I want to change www.domain1.com to www.domain2.com..
Would it be: find /root/test -type f -name \*.html|xargs perl -i.bak -pe 's/www.domain1.com/www.domain2.com/g' without any escape characters?

Cheers,
Hossam
Avatar of mBf

ASKER

Also, instead of "." should I enter the directory name as "/directory/path/" or "/directory/path/."?
find /root/test -type f -name \*.html|xargs perl -i.bak -pe 's/www\.domain1\.com/www.domain2.com/g'
# more perfect
Avatar of mBf

ASKER

When I test this script it works well, but when I use it in a real life situation it does not modify the files nor produce .bak ones. Very weird!

Any suggestions?
sounds like your real life data does not match your test requirements :-))
you have to change one of them ..
Avatar of mBf

ASKER

Got it.. For some reason this command does not follow symlinks.
Thanks, ahoffmann and much appreciated

--Hossam
for symlinks you have to check find's docs, symlinks, mount points and -prune are the dragons to beat on each platform :-(
Avatar of mBf

ASKER

Maybe on my vacation ;)