find . -name "*.java" | xargs sed -i 's/foo/bar/g'
or
find . -name "*.java" | xargs perl -pi -e's/foo/bar/g'
Question
I am trying to do a recursive find and replace in HP-UX and am missing something.
What I have at the moment:
find . -name "*.java" | xargs sed 's/foo/bar/g'
know the problem is that it's not replacing the file inline. I believe on other OS's there is a -i flag, but it seems in my HP-UX version, there isn't.
Any suggestions?
Solution
find . -name "*.java" | xargs sed -i 's/foo/bar/g'
or
find . -name "*.java" | xargs perl -pi -e's/foo/bar/g'
OTHER TIPS
The simple, portable solution to run any tool you like on the files and change them "in-place" (sed -i uses a tmp file behind the scenes too) is just:
find . -name "*.java" |
while IFS= read -r file; do
sed 's/foo/bar/g' "$file" > tmp && mv tmp "$file"
done
You can use that approach for sed, grep, cut, whatever you like:
find . -name "*.java" |
while IFS= read -r file; do
grep "whatever" "$file" > tmp && mv tmp "$file"
done
The only thing that won't work on is files whose names contain newlines but nor would the find | xargs
solution as-is and you should rename those if you have them anyway.