bash - when - windows remove all pyc files

How do I remove all.pyc files from a project? (12)

Django Extension

Note: This answer is very specific to Django project that have already been using Django Extension.

python clean_pyc

The implementation can be viewed in its source code.

I've renamed some files in a fairly large project and want to remove the .pyc files they've left behind. I tried the bash script:

 rm -r *.pyc

But that doesn't recurse through the folders as I thought it would. What am I doing wrong?

find . -name '*.pyc' -delete

Surely the simplest.

Add to your ~/.bashrc:

pyclean () {
        find . -type f -name "*.py[co]" -delete
        find . -type d -name "__pycache__" -delete

This removes all .pyc and .pyo files, and __pycache__ directories. It's also very fast.

Usage is simply:

$ cd /path/to/directory
$ pyclean

First run:

find . -type f -name "*.py[c|o]" -exec rm -f {} +

Then add:


To ~/.profile

Further, people usually want to remove all *.pyc, *.pyo files and __pycache__ directories recursively in the current directory.


find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf

I used to use an alias for that:

$ which pycclean

pycclean is aliased to `find . -name "*.pyc" | xargs -I {} rm -v "{}"'

In current version of debian you have pyclean script which is in python-minimal package.

Usage is simple:

pyclean .

Just to throw another variant into the mix, you can also use backquotes like this:

rm `find . -name *.pyc`

full recursive

ll **/**/*.pyc
rm **/**/*.pyc

if you don't want .pyc anymore you can use this single line in a terminal:


if you change your mind:


find . -name "*.pyc" -exec rm -f {} \;

find . -name "*.pyc"|xargs rm -rf