WordPress Mike
WordPress Mike

Reputation: 458

Sed Find and Replace PHP Files in Folders Named Themes

This script is working to find and replace a URLs in PHP files and works very fast when I run it in a test directory with only 1 file. But, I need to run it from the home directory on my server which is taking forever(at least 5 minutes so far.) It seems it would be faster if I could narrow the search to not all PHP files but only PHP files in "themes" folders. There are multiple "themes" folders throughout the server. Is is possible to narrow search and replace to only php files in folders named "themes"?

find . -type f -iname "*.php" -exec sed -i.bak 's/http:\/\/www.changed.com/http:\/\/www.stillchanged.com/g' {} \;

Example Paths to Themes Folder

/home/site1/public_html/wp-content/themes/

/home/site2/public_html/wp-content/themes/

/home/site3/public_html/wp-content/themes/

Upvotes: 0

Views: 211

Answers (2)

Suku
Suku

Reputation: 3880

find /tmp/* -type d -iname test\* -exec find {} -type f -iname test\* \;

You can do a nested find like above.

Example result:

sgeorge-mn:~ sgeorge$ find /tmp/* -type d -iname test\*
/tmp/stack/test_e
/tmp/stack/test_f
/tmp/stack/test_g
/tmp/stack/test_h
/tmp/stack/test_i
/tmp/stack/test_j
/tmp/stack/test_k
/tmp/test_a
/tmp/test_b
/tmp/test_c
/tmp/test_d
sgeorge-mn:~ sgeorge$ find /tmp/* -type d -iname test\* -exec find {} -type f -iname test\* \;
/tmp/stack/test_e/testa
/tmp/stack/test_e/testb
/tmp/stack/test_e/testc
/tmp/stack/test_e/testd
/tmp/stack/test_f/testa
/tmp/stack/test_f/testb
/tmp/stack/test_f/testc
/tmp/stack/test_f/testd
/tmp/stack/test_g/testa
/tmp/stack/test_g/testb
/tmp/stack/test_g/testc
/tmp/stack/test_g/testd
/tmp/stack/test_h/testa
/tmp/stack/test_h/testb
/tmp/stack/test_h/testc
/tmp/stack/test_h/testd
/tmp/stack/test_i/testa
/tmp/stack/test_i/testb
/tmp/stack/test_i/testc
/tmp/stack/test_i/testd
/tmp/stack/test_j/testa
/tmp/stack/test_j/testb
/tmp/stack/test_j/testc
/tmp/stack/test_j/testd
/tmp/stack/test_k/testa
/tmp/stack/test_k/testb
/tmp/stack/test_k/testc
/tmp/stack/test_k/testd
/tmp/test_a/testa
/tmp/test_a/testb
/tmp/test_a/testc
/tmp/test_a/testd
/tmp/test_b/testa
/tmp/test_b/testb
/tmp/test_b/testc
/tmp/test_b/testd
/tmp/test_c/testa
/tmp/test_c/testb
/tmp/test_c/testc
/tmp/test_c/testd
/tmp/test_d/testa
/tmp/test_d/testb
/tmp/test_d/testc
/tmp/test_d/testd

EDIT: In your case you even don't want a nested find:

Use find like following:

find /home/*/public_html/wp-content/themes/ -type f -iname \*.php

Upvotes: 1

Cozzamara
Cozzamara

Reputation: 1338

Add -wholename "*themes*" to the list of your conditions. That will match only files under 'themes' sub-directories (recursively).

Upvotes: 0

Related Questions