2

I would like to replace recursively (in a project directory) the word 'javax' with 'jakarta'. That works for files ending in "java":

find . -name '*.java' | xargs sed -i 's/javax/jakarta/g'

I'd need to do the same also for XML files. To use a single command, I've tried modifying the find expression as follows:

find . -regex '^.*\.(java|xml)$'

However, no files are found. I've tried as well with:

find . -regex '.*/^.*\.(java|xml)$'

But still nothing. Can you help me to rewrite the finder expression to use multiple files suffixes? Thanks

1
  • 1
    if your find supporst -regextype then you can choose which regex engine to use, now you're using -regextype posix-egrep Commented Sep 27, 2022 at 15:05

1 Answer 1

3

You can still use -name multiple times to match multiple pattern:

find . \( -name '*.java' -o -name '*.xml' \) -print0 |
xargs -0 sed -i 's/javax/jakarta/g'

If you want to use -regex then you should also use -regexrtype option for extended regex matching:

find . -regextype awk -regex '.+\.(java|xml)$' -print0 |
xargs -0 sed -i 's/javax/jakarta/g'

Take note of -print0 and xargs -0 options to take care of file names with special characters.

Sign up to request clarification or add additional context in comments.

2 Comments

-regexrtype is a GNU find extension; it will not work on platforms which have a different version of find
On BSD (osx) find this would be find -E . -regex '.+\.(java|xml)$'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.