Sign up ×
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 1 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
The problem with this command is that not all files necessarily have bak files so it would be nice to check up first only files which have bak files and then remove after verification. – Masi Jun 29 at 11:36

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.