Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How to remove all the .pyc files recursively from a certain directory including sub-directories? I tried

$rm -f *.pyc

This seems to work for only the current directory, but not the directories following it. Please help me..

share|improve this question

migrated from stackoverflow.com Nov 7 '12 at 13:59

This question came from our site for professional and enthusiast programmers.

3 Answers 3

up vote 1 down vote accepted

You can use

cd <your_directory>
find . -name "*.pyc" -exec rm -rf {} \;

This will remove all the *.pyc files from your current directory and its sub directory

share|improve this answer
I felt that this is a very simple command.. self explanatory.. –  Hisham Ibrahim Nov 7 '12 at 12:35
Why doesn't anyone think of using rm -rf *.pyc ? –  Nehal J. Wani Nov 10 '12 at 15:22
@NehalJ.Wani because that wouldn't work. rm does not have a search functionailty. the rm -rf can delete directories (and subdirectories recursively). But not search through subdirectories and delete only files. –  ypercube Jun 30 at 10:05
Thank you for the update –  Abhilash Joseph Jun 30 at 10:06

Use find:

find /some/directory -type f - name "*.pyc" -exec rm -f {} \;

or, if your find has the -delete option:

find /some/directory -type f - name "*.pyc" -delete
share|improve this answer
find /var/www/html -name "*.pyc" -delete
share|improve this answer
You need to quote *.pyc otherwise the shell will expand it. This will not work if there are pyc files in the base directory. –  dogbane Nov 7 '12 at 12:27
@dogbane thanks. added :) –  user868766 Nov 7 '12 at 12:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.