Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1015
  • Last Modified:

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
0
mBf
Asked:
mBf
  • 5
  • 4
1 Solution
 
ahoffmannCommented:
find . -type f -name \*.html|xargs perl -i.bak -pe 's/lamb/sheep/g'
0
 
mBfAuthor Commented:
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
0
 
mBfAuthor Commented:
Also, instead of "." should I enter the directory name as "/directory/path/" or "/directory/path/."?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ahoffmannCommented:
find /root/test -type f -name \*.html|xargs perl -i.bak -pe 's/www\.domain1\.com/www.domain2.com/g'
# more perfect
0
 
mBfAuthor Commented:
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?
0
 
ahoffmannCommented:
sounds like your real life data does not match your test requirements :-))
you have to change one of them ..
0
 
mBfAuthor Commented:
Got it.. For some reason this command does not follow symlinks.
Thanks, ahoffmann and much appreciated

--Hossam
0
 
ahoffmannCommented:
for symlinks you have to check find's docs, symlinks, mount points and -prune are the dragons to beat on each platform :-(
0
 
mBfAuthor Commented:
Maybe on my vacation ;)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now