Delete all files in a folder that don't match a certain file extension

find . -type f ! -name "*.foo" -name "*.bar" -delete
This command is recursive and will delete in all directories in ".". It will find and delete all files not specified with ! -name "pattern". In this case it's file extensions. -type f means it will only find files and not directories. Finally the -delete flag ask find to delete what it matches. You can test the command by running it first without delete and it will list the files it will delete when you run it.
Sample Output
enki 13:09:08 /tmp/foo/bar $ find . ! -name "*.xml" ! -name "*.js" -type f -delete
enki 13:12:23 /tmp/foo/bar $

By: sh1mmer
2010-10-07 20:17:38

9 Alternatives + Submit Alt

What Others Think

More than a decade ago, didn't knew I will still find this treasure here. Regards for everybody. see you!!
freykarts · 57 weeks and 6 days ago
Command-line fu website is about coding. We are the best London real estate consultants and suggest that if anyone wants a coding solution then connect with this website.
marrysmith · 52 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: