Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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 '15 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.