unix find and replace recursively

n00b0101
n00b0101 used Ask the Experts™
on
Hopefully, this will be a quick one for someone here... I need to find and replace a string recursively in unix.  

Normally, I use:

 
   perl -e "s/term/differenterm/g;" -pi $(find path/to/DIRECTORY -type f)

Open in new window


But, the string I need to replace contains slashes, and I'm not sure how to escape them?

So, I need to do:

    perl -e "s/FIND/REPLACE/g;" -pi $(find path/to/DIRECTORY -type f)

Open in new window

where FIND = '/string/path/term'
and REPLACE = '/string/path/newterm'
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can use

s###;

s#/string/path/term#/string/path/newterm#;

OR
s{/string/path/term}{/string/path/newterm};

Author

Commented:
I tried both of those, but it returns "Illegal variable name".

perl -e "s#/string/path/term/#/string/path/newterm/#" -pi $(find ./* -type f )
Illegal variable name.

Open in new window

What exactly are you trying to do?
Acronis in Gartner 2019 MQ for datacenter backup

It is an honor to be featured in Gartner 2019 Magic Quadrant for Datacenter Backup and Recovery Solutions. Gartner’s MQ sets a high standard and earning a place on their grid is a great affirmation that Acronis is delivering on our mission to protect all data, apps, and systems.

Author

Commented:
"
So, I need to do:

   
perl -e "s/FIND/REPLACE/g;" -pi $(find path/to/DIRECTORY -type f)

Open in new window

                                 
where FIND = /string/path/term
and REPLACE = /string/path/newterm
"
Distinguished Expert 2017

Commented:
perl -pi.bak -e 's/(\/string\/path\/)term/$1newterm/g;'  $( find /path/to/directory -type f)
The -i option requires a suffix to a maintain a backup.
Most Valuable Expert 2014
Top Expert 2015
Commented:
the command
  perl -e "s#/string/path/term/#/string/path/newterm/#" -pi $(find ./* -type f )
could give the error
  Illegal variable name.
if you were using it in csh
it should work in bash or ksh
in csh, you could use
  perl -e "s#/string/path/term/#/string/path/newterm/#" -pi `find ./* -type f `

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial