I want to cleanup a directory (and subdirectories) from each occurrance of the .py and .pyc files. I tried the following

find . -name '*pyc' -o -name '*py~' -exec rm -f {} \;

which left out all pyc files. When doing

find . -name '*pyc' -o -name '*py~'

the command finds the pyc files, which are not deleted, whatsoever. Any insights?

4 Answers 4


Instead of using the -o why not just do a wildcard pattern?

find . -name '*py[c~]' -delete


find . -name '*py[c~]' -exec rm -f {} +
  • 2
    Good point for this particular case. Note that it's not a regex, that's still a shell wildcard pattern. Apr 4, 2013 at 18:50
  • Ahh, I always understood them to be the same. Thanks for correcting that.
    – h3rrmiller
    Apr 4, 2013 at 22:26

The -a operator (implicit between two predicates) has precedence over -o, so you need parenthesis:

find . \( -name '*pyc' -o -name '*py~' \) -a -exec rm -f {} +

(I include the -a above for demonstration purpose only, it is not needed as it is implicit). I replaced ; with + as there's probably no point running one rm per file if that can be avoided.

  • 1
    Why don't you just use -delete?
    – Aif
    Apr 4, 2013 at 17:53
  • 6
    @Aif, because -delete is not a standard option. By all means, use it if your find supports it, but don't use it in scripts that are meant to be portable. Apr 4, 2013 at 17:56
  • find is a lot like Perl. Amazingly powerful, but write-only.
    – Plutor
    Apr 4, 2013 at 22:43
  • @StephaneChazelas Oh I didn't know! I have it under OSX on both find and gnu-find, so I though "if even BSD find has it, portable it must be!".
    – Aif
    Apr 5, 2013 at 10:33

If you have bash 4+, you don't even need find, since globstar will give you recursive globbing:

shopt -s globstar
for f in ./**/*.py{c,~}; do rm "$f"; done

Note that shopt needs to be on its own line, rather than separated by a ; (and should be used at the very top of a script, directly below the shebang if possible).


I know that the question it's not so new... but maybe it was enough to use

rm -f `find . -name '*.pyc' -o -name '*.py~' `

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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