Quick Command Line Tip - Recursively Delete Files of a Certain Type

Applications can create a lot of temporary files sometimes, and these files aren't always cleaned up automatically.

An example of this is when you run Python applications. Particularly if you're a Python developer, your source code directories stack up with a .pyc version of each file, which is the cached compiled copy of the script.

To clean up (especially if you're going to do a source commit or an upload somewhere to extend that example) files of a certain file extension, you can use this command line snippet:

$ find . -name "*.ext" -exec rm '{}' ';'

Obviously, replace *.ext with the pattern that you want to delete.

I shouldn't need to say this, but use this with caution. Make sure you're not accidentally going to delete something useful that matches the pattern you enter, and always keep backups yada yada. Tread carefully when batch deleting.

Avatar for peter Peter Upfold - http://peter.upfold.org.uk/

Peter Upfold is a technology enthusiast from the UK. Peter’s interest in Linux stems back to 2003, when curiosity got the better of him and he began using SUSE 9.0. Now he runs Linux Mint 9 on the desktop, runs a CentOS-based web server from home for his personal website and dabbles in all sorts of technology things across the Windows, Mac and open source worlds.

Home » Articles »

Discussion: Quick Command Line Tip - Recursively Delete Files of a Certain Type

  1. Linuxy (guest)

    # Posted on 19 June 2008 at 05:40 PM

    Why not simply use:

    $ find . -name "*.ext" -delete



  2. Binny V A (guest)

    # Posted on 19 June 2008 at 06:15 PM

    I use this method all the time to delete .svn folders. I use the command... find . -name .svn -exec rm -rf {} \;

    The -delete option will not work in this case.



  3. Pantelis (guest)

    # Posted on 20 June 2008 at 07:54 AM

    I have another favorite

    $ rm -rf find . -name "smth"



  4. Carlos (guest)

    # Posted on 20 June 2008 at 08:39 AM

    $ find . -name "*.ext" | xargs rm



  5. jono (guest)

    # Posted on 20 June 2008 at 10:21 AM

    why not

    $ rm -R /dir *.ext

    ??



  6. Carlos (guest)

    # Posted on 20 June 2008 at 03:44 PM

    rm can fail if the number of files is large.



  7. libkarl2 (guest)

    # Posted on 20 June 2008 at 05:25 PM

    for large file lists, try..

    $ find /dir -name '*.ext' -print | xargs -r rm

    that way, you aren't exec(3)ing rm for every file..



  8. Matt (guest)

    # Posted on 21 June 2008 at 05:27 AM

    Thank you so much! That was just what I was looking for. I should really learn how to use the find command...



  9. # Posted on 22 June 2008 at 11:36 AM

    [...] Applications can create a lot of temporary files sometimes, and these files aren’t always cleaned up automatically. An example of this is when you run Python applications. Particularly if you’re a Python developer, your source code directories stack up with a .pyc version of each file, which is the cached compiled copy of the script. Read more at FOSSwire [...]



Home » Articles » Quick Command Line Tip - Recursively Delete Files of a Certain Type

Sign In