Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Command for Find/Replace in text files (inc. files in sub-folders)

    Hi I found this command that works great finding and replacing a simple string to another in files located in that folder and all sub-folders.

    find . -name '*.php' | xargs perl -pi -e 's/OldText/NewText/g'
    The problem I have is that I need to replace a more complex string, like this:
    Old string: /mnt/stor6-wc2-dfw1/627896/982574/
    New string: /mnt/stor8-wc2-dfw1/369587/302589/

    There I don't know how to do it... since the / is what separates the old from the new strings, and the strings that I want to replace have / in it.

    Also, I would like to know how to specify under what folder replace the files, for example, I want that it search/replaces all files under /var/www/mysite/htdocs folder.

    All help is welcomed... thanks!

  2. #2
    Change the / separator to a character that doesn't appear in the strings like |:

    find . -name '*.php' | xargs perl -pi -e 's|OldText|NewText|g'
    Use the -mindepth 2 option. The find would look like:

    find /var/www/mysite/htdocs -mindepth 2 -name '*.php' | xargs perl -pi -e 's|OldText|NewText|g'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts