delete all files except one or some

Delete All Files Except One in Linux

Sometimes you may need to delete all files except one or some in Linux. In this article, we will learn different ways to remove all files in folder except one or some in Linux. You can use these commands in all Linux distributions since they are universally available.


Delete All Files Except One in Linux

Here are the different ways to delete files with exclusions. We will mainly use different patterns to match the files to be excluded. Here are the different kinds of patterns that can be used.

  • *(pattern-list) – matches zero or more occurrences of the specified patterns
  • ?(pattern-list) – matches zero or one occurrence of the specified patterns
  • +(pattern-list) – matches one or more occurrences of the specified patterns
  • @(pattern-list) – matches one of the specified patterns
  • !(pattern-list) – matches anything except one of the given patterns

But in order to be able to use regex patterns with other tools like rm command, we need to enable extglob shell extension, with the following command.

# shopt -s extglob

Once you have enabled the above extension, you can use regex patterns with other shell commands like rm.

If you want to turn off this extension, you can do so with the following command.

$ shopt -u extglob


1. Using rm command

We will look at different use cases to delete files with exclusions. Here is the command to delete all files in your folder except the one with filename data.txt. Replace it with the filename of your choice.

$ rm -v !("data.txt")

Here is the command to delete all files in your folder, except files data.txt and project.txt.

$ rm -v !("data.txt"|"project.txt") 

In the above example, we have included the two filenames, separated by pipe (|). Please make sure to add quotes around each filename separately, as shown above.

Here is the command to delete all files, except .txt ones.

$ rm -v !(*.txt)

Combining the previous commands, here is the command to delete all files excluding .txt and .pdf files.

$ rm -v !(*.txt|*.pdf)


2. Using find command

You can also delete files using find command as shown below. Here are the 3 ways to delete all files except ones whose filenames match a given pattern. Replace PATTERN below with the regex pattern that matches filenames that need to be excluded from deletion.

$ find /directory/ -type f -not -name 'PATTERN' -delete
$ find /directory/ -type f -not -name 'PATTERN' -print0 | xargs -0 -I {} rm {}
$ find /directory/ -type f -not -name 'PATTERN' -print0 | xargs -0 -I {} rm [options] {}

In first command, the find command itself deletes the file, using -delete option. In the next 2 commands, the find command passes its output to xargs command which constructs separate rm command for those files.

Here is a simple command to find and delete all files in your folder except .zip files.

$ find . -type f -not -name '*.zip'-delete

You may also delete files except .zip files using the following command.

$ find . -type f -not -name '*zip' -print0 | xargs -0  -I {} rm -v {}

Here is a command to delete files excluding .zip and .pdf files.

$ find . -type f -not \(-name '*zip' -or -name '*pdf' \) -delete


3. Using GLOBIGNORE

GLOBIGNORE is a shell variable where you can store different filenames and extensions in a colon-separated manner, so that they are excluded from deletion. Here is an example to exclude .zip and .pdf files from deletion.

$ cd test
$ GLOBIGNORE=*.zip:*.pdf

Now when you run rm command in any folder location, it will not delete .zip and .pdf files. You can always turn off GLOBIGNORE with the following command.

$ unset GLOBIGNORE

Please note, the above GLOBIGNORE setting will work only as long as your session lasts. If you want to make it permanent, you need to add it to ~/.bashrc file as environment variable.

In this article, we have learnt how to delete files except one or more files in Linux.

Also read:

How to Mount Remote Directory in Linux
How to Check Bad Sectors in Linux
How to Convert Markdown to HTML in Linux
How to Create SFTP User for Specific Folder
How to Open File Dialog Box in Python

Leave a Reply

Your email address will not be published. Required fields are marked *