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 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?

share|improve this question

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.

share|improve this answer
Thanks you very much; it seems to work... – Alex Apr 4 '13 at 17:52
Why don't you just use -delete? – Aif Apr 4 '13 at 17:53
@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. – Stéphane Chazelas Apr 4 '13 at 17:56
find is a lot like Perl. Amazingly powerful, but write-only. – Plutor Apr 4 '13 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 '13 at 10:33

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

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


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

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).

share|improve this answer

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~' `
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.