Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

EDIT: I found the solution. This command works, in case someone finds it useful. The i.bak modifier makes a backup of every changed file.

find /pathoffilestochange -name "*.php" | xargs sed 's|/wrong/path/|/correct/path/|g'

Hello,

I want to change a path inside dozens of .php files. I dont know how to use sed, but I got this command somewhere on the internet before and have succesfully used it.

 find /ruta -name "*.txt" | xargs sed -i.bak 's/charset=Foo/Bar/g'

Except now I need to change the delimiters to a different character, hece the @. This is my current command:

find /pathoffilestochange  -name "*.php" | xargs sed -i.bak 's@charset=/wrong/path/@/correct/path/@g'

It is not working, and it doesnt give any errors, but I check the files and they remain the same.

I am executing the command as root, which is the file owner, and the permissions seem to be OK, so that must not be the issue. I am running Debian.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Just replacing the reserved delimiter with @ won't work. What you have to do is to escape your slashes like this \/ With the \ character, you cancel the special meaning of a character in a regular expression.

So, your command needs to look like this:

find /pathoffilestochange -name "*.php" | xargs sed -i.bak \ 's/charset=\/wrong\/path\//\/correct\/path\//g'

It's confusing, but that's how regexes are working.

By the way, with the single backslash after -i.bak I canceled the meaning of the new line character, so I could this command on two lines for easier readability.

share|improve this answer
1  
No, you don't need to escape the slashes if you change the delimiter to something else. –  Ignacio Vazquez-Abrams Feb 9 '11 at 17:25
    
Yes, you are right, and it kind of makes sense to do this for a pathname, but I prefer the escape route, mostly out of habit (the "that's the way I learned it" habit ;)) and to prevent confusion because so I always have to escape this character. –  SvW Feb 9 '11 at 17:37

What version of sed are you using? Works for me:

$ cat foo 
hello charset=/wrong/path/fcc yes
$ find . -name foo | xargs sed -i.bak 's@charset=/wrong/path/@/correct/path/@g'
$ cat foo
hello /correct/path/fcc yes
$ sed --version
GNU sed version 4.2.1
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.