I'm using macOS X, inputing this into the terminal to rename some stuff:

find /Users/Ad//Desktop/Project/src/ -type f -name '*.java' -exec perl -pi -w -e 's/getGreenObject()/newObject("green")/g;' {} \;

Which should rename all:




but What it did was replace getGreenObject() with this:


which has resulted in tons of errors. It seems the command doesn't recognize "()" the open&close brackets?

How can I fix all these errors now with the same command?


The parentheses are being treated as part of the regular expression syntax (they indicate that their contents should be treated as a group) rather than as literal characters to be matched. To tell the regular expression parser to treat them as literal characters, you can escape them with backslashes:

find /Users/Ad//Desktop/Project/src/ -type f -name '*.java' -exec perl -pi -w -e 's/getGreenObject\(\)/newObject("green")/g;' {} \;

Now: do you have a backup from before this happened, so you can go back and try again? When doing any mass change like this, you should always make sure your backup is up to date (you have a backup, right?), and also run a small test before turning it loose on your main code base.

If you don't have a backup to go back to, you can probably fix it like this:

find /Users/Ad//Desktop/Project/src/ -type f -name '*.java' -exec perl -pi -w -e 's/newObject\("green"\)\(\)/newObject("green")/g;' {} \;

Note that I said probably. The same recommendations apply here: make sure you have an up-to-date backup first, and test it on a few files first. I am not responsible if this irreversibly shreds your code base.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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