Remove files recursively in Linux

11,511

Solution 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

Solution 2

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

Solution 3

find /var/www/html -name "*.pyc" -delete
Share:
11,511

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

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

  • dogbane
    dogbane over 11 years
    You need to quote *.pyc otherwise the shell will expand it. This will not work if there are pyc files in the base directory.
  • Admin
    Admin over 11 years
    I felt that this is a very simple command.. self explanatory..
  • Nehal J Wani
    Nehal J Wani over 11 years
    Why doesn't anyone think of using rm -rf *.pyc ?
  • ypercubeᵀᴹ
    ypercubeᵀᴹ almost 9 years
    @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.