Adam Michael Roach's picture

Recursively Delete all Files in a Directory with a Specific File Extension

This is pretty straight forward. You have a directory with a large amount of files with a particular extension that you want to recursively get rid of. For example, you back up a directory from a Windows machine onto an external hard drive. When you mount that drive on a *nix box you are going to see a bunch of files throughout the directory structure such as thumbs.db, desktop.ini, etc. I don't need that and want to see them anymore. The following commands will solve the problem:

WARNING: DO NOT RUN THE FOLLOWING COMMANDS AS ROOT UNLESS YOU ARE ABSOLUTELY SURE OF WHAT YOU ARE DOING. THESE COMMANDS RESULT IN A LOSS OF DATA AND MAY NOT BE RECOVERABLE.

Two commands here. The first one will delete all of the files without prompting for deletion. The second one will prompt you.

find /path/to/scan/ -iname "*." -exec rm '{}' ';'

find /path/to/scan/ -iname "*." -ok rm '{}' ';'

Have a better way to do it? Leave me a comment below.

Related:

Post new comment

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can enable syntax highlighting of source code with the following tags: [bash], [drupal5], [drupal6], [javascript], [php], [python], [ruby].
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options