Recursively Delete Files from Command Line

By  on  

I'm one of those people who can't stand a messy laptop;  I don't keep around files I don't need and I refuse to install apps unless I absolutely need them.  Unfortunately Mac OS X and Windows generate files whenever they like, like .DS_Store and Thumbs.db.  Sure they serve their purpose but that doesn't mean the clutter doesn't annoy me.

If you want to recursively find delete files you don't want, there's a simple way to do that:

find . -name '.DS_Store' -type f -delete

You can use * as a wildcard too:

find . -name '*.zip' -type f -delete

Of course my cleanup only lasts a short time, but hey -- you can use this command for more intelligent purposes!

Recent Features

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

  • By
    CSS 3D Folding Animation

    Google Plus provides loads of inspiration for front-end developers, especially when it comes to the CSS and JavaScript wonders they create. Last year I duplicated their incredible PhotoStack effect with both MooTools and pure CSS; this time I'm going to duplicate...

Incredible Demos

  • By
    MooTools Documentation Search Favelet

    I'm going to share something with you that will blow your mind: I don't have the MooTools documentation memorized. I just don't. I visit the MooTools docs frequently to figure out the order of parameters of More classes and how best to use...

  • By
    MooTools ContextMenu Plugin

    ContextMenu is a highly customizable, compact context menu script written with CSS, XHTML, and the MooTools JavaScript framework. ContextMenu allows you to offer stylish, functional context menus on your website. The XHTML Menu Use a list of menu items with one link per item. The...

Discussion

  1. Tolga

    Interestingly. I only knew about -exec and xargs.

  2. Hristo
    rm find . -name '.DS_Store'
  3. I use Asepsis for .DS_Store files: http://asepsis.binaryage.com/

  4. Just to mention that the order of the -delete flag is very important. Putting -delete flag first will make find try to delete everything below the specified starting point.

  5. If you’re on Windows, the following will work the same:

    del /s .DS_Store
    del /s Thumbs.db
    
  6. Using -iname instead of -name will ignore case.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!