A shell-based solution could also work, since the task here is easy enough (simply appending a string fragment to a filename)
for file in *.py; do newname="$file.backup"; mv "$file" "$newname"; done
If it is possible that no such file exists, you would need to catch that case explicitly. Assuming bash
, you could use the nullglob
option:
shopt -s nullglob; for file in *.py; do newname="$file.backup"; mv "$file" "$newname"; done
or test explicitly if the file exists:
for file in *.py; do newname="$file.backup"; [ -f "$file" ] && mv "$file" "$newname"; done
If the files in question can also be in sub-directories, things get more complicated. With bash
v4 onwards, you can use the globstar
option:
shopt -s globstar; for file in **/*.py; do newname="$file.backup"; mv "$file" "$newname"; done
Again, you may have to combine with shopt -s nullglob
to account for the case no such file exists.
find . -type f -name "*.py" -exec mv {} {}.backup \;
and if it doesn't work, please update the question with the exact error/reason.