---

How to Delete All Files in a Folder Except Certain Extensions

Sometimes, you may find yourself in a situation where you need to delete all files in a directory or simply clean up a directory by removing all files except those with a specific extension (e.g., files ending with a particular type).

In this article, we will show you how to delete files in a directory, excluding certain file extensions or types, using the rm, find, and globignore commands.

Before we move any further, let us start by briefly having a look at one important concept in Linux – filename pattern matching, which will enable us to deal with our issue at hand.

In Linux, a shell pattern is a string that consists of the following special characters, known as wildcards or metacharacters:

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends, & analysis