Super User is a question and answer site for computer enthusiasts and power users. 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

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 Nov 7 '12 at 13:59

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

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 '15 at 10:05
Thank you for the update – Abhilash Joseph Jun 30 '15 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

You must log in to answer this question.