Skip to content

Remove All Files Without Certain Extensions

Categories: Uncategorized

Table of Contents

I needed to remove all files in a hierarchy of folders that didn’t have certain extensions. I searched the net for a quick & dirty bash script to do this, but all the scripts I found only removed files in the current directory, I needed to remove files in the current directory and child directories. Heres the bash script I came up with.

cd ~/Desktop/targetDir
find . | egrep -v "(.css|.html|.htm)" | xargs rm

Assuming ~/Desktop/targetDir is the root directory you want to remove files from, and you don’t want files ending in .css, .html, or .htm. NOTE: if your file paths contain spaces this command will probably fail/mess something up. I take no responsibility for anything that happens to your computer because of this script. Hopefully somebody might find this useful.