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

  • June 19, 2008
  • Avatar for peter
    Peter
    Upfold

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

Home » Articles »