Operating System - HP-UX
1825001 Members
2491 Online
109678 Solutions
New Discussion юеВ

Re: grep for string and change in a whole directory

 
SOLVED
Go to solution
Ratzie
Super Advisor

grep for string and change in a whole directory

I need to set up alittle script ( I am rearranging my directory structure ) that searches thru a directory and changes any /home/test/pub strings in my files to ... /home/test/foo/moo
3 REPLIES 3
Rodney Hills
Honored Contributor
Solution

Re: grep for string and change in a whole directory

Try-

cd /directory
perl -ipe 's{/home/test/pub}{/home/test/foo/moo}g;' *

perl has a facility to do in place updating, where each file will be changed and the result will be put back in the original file.

HTH

-- Rod Hills
There be dragons...
Sanjay_6
Honored Contributor

Re: grep for string and change in a whole directory

Hi,

If you want to do this for every file including files in the subdirectories, then try,

find /source_dir -type f -exec sed 's/\/home\/test\/pub/\/home\/test\/foo\/moo/g' {} \;

Hope this helps.

Regds
Ratzie
Super Advisor

Re: grep for string and change in a whole directory

Perl works great!