Search replace string in files in directories

Discussion in 'Site & Server Administration' started by kokopoko, Feb 12, 2007.

  1. #1
    I need to do a search and replace on my Unix server. I need to find string A in all .php .html files and replace it with string B down through subdirectories too.

    I am not sure how to do this. Has anyone else had to do this before? Find and grep?

    Christine
     
    kokopoko, Feb 12, 2007 IP
  2. Joobz

    Joobz Peon

    Messages:
    598
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do you have a local copy of your site? It's very easy to do locally, but there are many programs out there that will do search/replace via ftp
     
    Joobz, Feb 12, 2007 IP
  3. grandpa

    grandpa Active Member

    Messages:
    185
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #3
    windows i use this:
    powergrep.com
     
    grandpa, Feb 12, 2007 IP
  4. kokopoko

    kokopoko Active Member

    Messages:
    238
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    kokopoko, Feb 12, 2007 IP
  5. ThreeGuineaWatch

    ThreeGuineaWatch Well-Known Member

    Messages:
    1,489
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    140
    #5
    Hi,

    Make sure you have a backup before you play about with this as an inopportune typo in the command can make a mess of your files. Also, make sure you are in the correct directory when you issue the command!

    Assuming you have shell access and read/write access to all the applicable files...

    $ find . \( -name "*.php" -or -name "*.html" \) | xargs grep -l StringA | xargs sed -i -e 's/StringA/StringB/g'
    Code (markup):
    Cheers,
     
    ThreeGuineaWatch, Feb 15, 2007 IP