0

I'm in the process of re-structuring my website & have to re-arrange references within files. There is a directory that contains many sub-directories & php files. I'm after some kind of a command/script that can search for each & every file in that directory for this pattern & replace it with a custom pattern.

For Eg: i want to replace exact occurrences of one/two/three/ by /shopping/shop/ipad . In other words if any file contains some string For Eg:

location:index.php?p=one/two/three/abcdefg&aid

then it should be replaced by

location:index.php?p=/shopping/shop/ipad/abcdefg&aid

Note the content replacement should take place inside every file, in every directory / sub directory within a main directory (say the main directory name is abc & all sub directories & files are within abc). The directory names themselves should not be changed. How can i achieve this ? My server runs linux & i have root & shell access.

Trent Earl
  • 3,517
  • 1
  • 15
  • 20
Anita
  • 117
  • 2
  • 9
  • possible duplicate of [Awk/Sed: How to do a recursive find/replace of a string?](http://stackoverflow.com/questions/1583219/awk-sed-how-to-do-a-recursive-find-replace-of-a-string) – tripleee Aug 15 '15 at 15:15

3 Answers3

1

sed is what you are looking for:

find .  -exec sed -i "s/one\/two\/three/shopping\/shop\/ipad/g" '{}' \;
Trent Earl
  • 3,517
  • 1
  • 15
  • 20
1
find . -type f|xargs perl -pi -e 's/one\/two\/three/shopping\/shop\/ipad/g'

tested and working fine.and it also doesnot replace if there is o nly one

you can also find it here

Vijay
  • 65,327
  • 90
  • 227
  • 319
0

Try this:

find . -type f -print0 | xargs -0 sed -i 's/one\/two\/three/shopping\/shop\/ipad/g'
Vijay
  • 65,327
  • 90
  • 227
  • 319
knesenko
  • 72
  • 2