Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a directory in which are listed some .py files and some .py.bak files. I want to delete the .py files and restore the backup file renaming them *.py. Is there a shell script that can do this?

Thank you,

share|improve this question
I don't mean to sound like a busybody, but it sounds like something you should be using version control for. –  Faheem Mitha Mar 21 '11 at 18:02

1 Answer 1

up vote 2 down vote accepted

This should do. Be sure to test on files you wouldn't mind losing.


rm -f *.py
for fn in *.py.bak
  mv "${fn}" "${fn%%.bak}"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.