Home Linux Commandline Tips How to Delete All Files in a Directory Except Few

How to Delete All Files in a Directory Except Few

File deletion is an important yet sensitive aspect of Linux administration. The Linux commands behind file deletion operations help us get rid of files we do not need.

There is usually a high chance that the files we wish to delete reside in a single folder or directory. If we did not need all these files; once we navigate to that directory, it would be easier to get rid of them with a simple rm command implementation.

$ cd directory
$ rm *
or 
$ sudo rm *

However, you might find yourself in a circumstance where the number of directory files you need to delete exceeds the number of directory files you wish to preserve.

This scenario requires some careful steps into the implementation of the appropriate Linux command so as not to accidentally delete important files like usable log files.

This article guide is here to address this issue.

Problem Statement

Since this article seeks to demonstrate the deletion of multiple files within a directory except for a few, we need to create some dummy files for demonstration purposes and also to make the tutorial a bit exciting.

$ touch file1.txt file2.txt file3.zip file4.txt file5.html file6.css  
Create Multiple Files in Linux
Create Multiple Files in Linux

Remove Files Using the Linux rm Command

As per its man page ($ man rm), the rm command belongs to the GNU Coreutils package and is primarily used for the deletion/removal of unwanted files and directories from a file system.

The basic syntax for the rm command is as follows:

$ rm [option]… [file]…

Some useful command options associated with the rm command include -v (--verbose) which is primarily helpful to users that need visual proof of the objectives achieved after the command executes.

$ rm -v sample.txt
Remove File in Linux
Remove File in Linux

The other useful rm command option is -f (--force) which ignores arguments or files specified for deletion if they are nonexistence.

$ rm -f sample.txt

For this tutorial, we will focus more on the --verbose option to successfully visualize the deletion of targeted files.

Activate Extended Pattern Matching

We will need to implement some pattern matching to be executed alongside the rm command if we want to be successful in meeting the objective of this article guide.

We will use the Linux shopt command to enable extglob which gives us the needed extended pattern matches to use alongside the rm command.

$ shopt -s extglob

In some Linux OS distributions, the above option is already active by default.

Option 1: Delete All Files and Reserve One

In this case, we will delete all the created files except for file1.txt.

$ rm -v !(file1.txt)
Delete All Files Except One
Delete All Files Except One

Option 2: Delete All Files and Reserve a Few

Let us try to delete all the files and leave three (file1.txt, file3.zip, and file5.html).

$ rm -v !(file1.txt|file3.zip|file5.html)
Delete All Files Except Few
Delete All Files Except a Few

Option 3: Delete All Files Except File Extension

Let us try to delete all the files and leave css, html, and zip files.

$ rm -v !(*.css)
$ rm -v !(*.html|*.zip)
Delete All Files Except File Extension
Delete All Files Except File Extension

You might also like to read the following related articles:

You can now flexibly sort and delete files in a directory in Linux via the rm command.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.