1

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

3 Answers 3

1

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

3
  • 1
    I felt that this is a very simple command.. self explanatory..
    – Hisham Ibrahim
    Nov 7, 2012 at 12:35
  • Why doesn't anyone think of using rm -rf *.pyc ? Nov 10, 2012 at 15:22
  • 1
    @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. Jun 30, 2015 at 10:05
5

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
2
find /var/www/html -name "*.pyc" -delete
1
  • 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, 2012 at 12:27

You must log in to answer this question.