LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   using sed + find in all user folders at one time (https://www.linuxquestions.org/questions/programming-9/using-sed-find-in-all-user-folders-at-one-time-4175503947/)

rizzoid 05-04-2014 10:14 PM

using sed + find in all user folders at one time
 
Any help is appreciated. No rush.

I found that I could use the sed command on my CentOS server to replace certain strings in the current folder, but I'd like one elegant command to do it across multiple directories.

The working sed command looks similar to this:

sed -i "s/smtp.live.com/mail.newone.com/g" myfile.php

And it worked well to change smtp settings in a specific file that is in every directory such as /home/user/directory. But I have to change directories for every edit.

How can I add to the sed command to make this work in one line?

The following test didn't work:
find /home/*/directory -name \myfile.php -exec | sed -i "s/smtp.live.com/mail.newone.com/g" myfile.php

Thanks.

Rizzoid

NevemTeve 05-04-2014 10:33 PM

Almost good.
Code:

find /home -name myfile.php -exec echo sed -i "s/smtp.live.com/mail.newone.com/g" {} \;
try it, and if it seems good, remove the 'echo'

rizzoid 05-05-2014 06:25 AM

It doesn't error, but it doesn't actually write to the file. The permissions are 755. Do I need to make them 777?

pan64 05-05-2014 06:30 AM

have you tried to remove echo ?

rizzoid 05-05-2014 06:36 AM

Yes, I'm seeing in the output that it is appears to change the file to:

-> public $smtphost = 'mail.newone.com';

But when I check the actual file it is not rewritten. Very strange!

pan64 05-05-2014 06:39 AM

probably you missed the flag -i ? (sed -i "s/ ...)

rizzoid 05-05-2014 06:42 AM

That was it! I removed the -i while testing. Awesome. Thank you so much pan64!!

Rizzoid

pan64 05-05-2014 06:43 AM

glad to help you
if you really want to say thanks just press YES (and do not forget NevemTeve)

rizzoid 05-05-2014 07:01 AM

One more question if you have time. I know that special characters can be a problem. How would I properly use backslash to change a password like this?

sed -i 's/FG;{U-[}-b~2c/newpass/g'

I know the [ will be a problem. Thanks again. No rush

Rizzoid

pan64 05-05-2014 07:07 AM

use \ before [


All times are GMT -5. The time now is 11:07 AM.