Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm using the following command on my mac:

$find . -name “*.java” -exec sed -i ’s/foo/bar/g’ {} \;

and it seems to have no effect.

I have two files in the directory that end in .java, which both have the foo text in them. Am I missing something?

EDIT : Results from request of comments

[aafghani-03:~/test amirafghani]$ find . -name "*.java" -exec sed -i 's/foo/bar/g' {} \;
sed: 1: "./bar.java": invalid command code .
sed: 1: "./foo.java": invalid command code .
share|improve this question
2  
Is that exactly what you're running? What if you remove the -exec and everything after it? Does it print the names of the Java files? (I'm worried that you're using smart quotes instead of ASCII quotes.) –  Mikel Apr 18 '12 at 19:05
1  
Actually, that solves it, at least for me. Both double and single quotes should be substituted by " and ', respectively. –  Lev Levitsky Apr 18 '12 at 19:07
1  
Smart(?) quotes! .. :) –  Peter.O Apr 18 '12 at 19:09
    
What I pasted was exactly what I was running. Can someone post an answer? –  Amir Afghani Apr 18 '12 at 20:17
1  
Next time please post the error message you are seeing when you ask the question. It will save time for everyone. –  Mikel Apr 19 '12 at 2:43

1 Answer 1

up vote 9 down vote accepted

First of all, make sure you are using regular ascii quotes like " and ' (ascii codes 0x22 and 0x27, respectively) in shell scripts, because the example in your post looks different (copy & paste from somewhere), and won't work that way.

Since you are on a Mac, you most probably have the BSD implementation of sed, in which case you have to write the command this way:

find . -name "*.java" -exec sed -i '' s/foo/bar/g \;

In the BSD implementation of sed the -i flag needs an argument: the extension of a backup file. For example with -i .bak the command would backup file1.txt as file1.txt.bak first before performing the replacement in the original file. Using an empty argument '' means to not use a backup file, which seems to be what you want.

The same thing in the GNU implementation would be:

find . -name "*.java" -exec sed -i s/foo/bar/g \;

Thank you @bahamat and @Mikel for improving my answer with your comments.

share|improve this answer
    
But you can pass an empty extension: find . -name \*.java -exec sed -i '' 's/foo/bar/g' {} \; –  bahamat Apr 18 '12 at 23:17
    
I also think sed -i -e 's/foo/bar/g' will work. Or at least it does on FreeBSD. (According to the Mac OS X man page, Apple uses FreeBSD's version of sed.) –  Mikel Apr 19 '12 at 4:15
    
@bahamat and Mikel you are absolutely right, thanks for pointing out, will update the answer now! –  janos Apr 20 '12 at 7:13

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.