Recursively deleting files (based on regex)

Published on Sept. 9, 2008, 1:20 a.m.

While we're on the topic... (the topic being recursively doing stuff to files), I often want to delete all the files in a hierachy of directories that match a regular expression (or regex).

The typical scenario for this is, "I want to delete all of my compiled python files."

Here's how I do it:
find ./ -type f -name "*.pyc" -exec rm {} \;
find ./ -type f -name "*.pyc" should find all files ending in .pyc under the current directory. Once they've been found, the -exec rm {} \; will remove them.
comments powered by Disqus