Anita
Anita

Reputation: 117

How to Replace a string pattern with another string pattern inside all files in directories and subdirectories

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.

Upvotes: 0

Views: 1169

Answers (3)

knesenko
knesenko

Reputation: 72

Try this:

find . -type f -print0 | xargs -0 sed -i 's/one\/two\/three/shopping\/shop\/ipad/g'

Upvotes: 0

Vijay
Vijay

Reputation: 67221

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

Upvotes: 1

Trent Earl
Trent Earl

Reputation: 3607

sed is what you are looking for:

find .  -exec sed -i "s/one\/two\/three/shopping\/shop\/ipad/g" '{}' \;

Upvotes: 1

Related Questions