I'm currently learning bash, and came upon the find
command. I'd like to use it to, say, find and rename all .py files to .bak (for example, hello.py should be renamed hello.bak).
I think I should be able to write a command like this:
find -name "*.py" -exec mv {} NEW_NAME \;
where NEW_NAME stands for the renamed {}.
I've tried various convoluted approaches, amongst which
assigning {} to a variable: I'm unable to do a variable assignment (
-exec a=1
for example returns an error)piping commands, but even though
echo $(echo ./hello.py | cut -d'.' -f2).bak
returns/hello.bak
as expected, replacing NEW_NAME with$(echo {} | cut -d'.' -f2).bak
doesn't.Finally (thanks to the following link: Changing arguments of {} find exec ), I was able to achieve my goal with
find -name "*.py" -exec bash -c 'mv $0 ${0/.py/.bak}' "{}" \;
I have a feeling though that there's a simpler way to do this, and that I'm overlooking something obvious.
Edit: I'm aware there are other ways to batch rename files than with find ... -exec ...
; I'm specifically looking for a better/simpler way to do this with this command (I was hoping for some Linux equivalent to Python's .replace()
string method).