So I've arrived at the conclusion that for recursively replacing all instances of a string in directory (only for .java extensions files) I need to use

find . -type f -name "*.java" -exec sed -i 's/original/result/g' {} +

However what do I do if the string I am trying to replace contains /?

For example I want to replace string /*Comment*/ with a couple normal words, what delimiters should I use, so that sed works properly?


Escape the slash, writing \/ You could also do:

perl -pi -e 's!original!result!g' *java

using ! as a delimiter instead of /. This is a bit shorter than using find & sed.

  • but not entirely equivalent of find + sed/awk/perl combo as this one works only for files in current directory while find will work recursively
    – Sundeep
    Nov 18 '16 at 12:21

You can use any delimiter using sed too (not only with perl as @vesa-karjalainen suggested), e.g.

find . -type f -name "*.java" -exec sed -i 's:original:result:g' {} +

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.