Linux: Bash Delete All Files In Directory Except Few

by on June 3, 2014 · 13 comments· LAST UPDATED June 5, 2014

in , ,

I'm a new Linux system user. I need to cleanup in a download directory i.e. delete all files from ~/Downloads/ folders except the following types:

*.iso - All iso images files.
*.zip - All zip files.

How do I delete all file except some in bash shell on a Linux, OS X or Unix-like systems?

Bash shell supports rich file pattern matching such as follows:

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsBash
Estimated completion time2m

  • * - Match any files.
  • ? - Matches any single character in filenames.
  • [...] - Matches any one of the enclosed characters.

Method #1: Say hello to extended pattern matching operators

You need to use the extglob shell option using the shopt builtin command to use extended pattern matching operators such as:

  1. ?(pattern-list) - Matches zero or one occurrence of the given patterns.
  2. *(pattern-list) - Matches zero or more occurrences of the given patterns.
  3. +(pattern-list) - Matches one or more occurrences of the given patterns.
  4. @(pattern-list) - Matches one of the given patterns.
  5. !(pattern-list) - Matches anything except one of the given patterns.

A pattern-list is nothing but a list of one or more patterns (filename) separated by a |. First, turn on extglob option:

 
shopt -s extglob
 

Bash remove all files except *.zip and *.iso files

The rm command syntax is:

## Delete all file except file1 ##
rm  !(file1)
 
## Delete all file except file1 and file2 ##
rm  !(file1|file2)
 
## Delete all file except all zip files ##
rm  !(*.zip)
 
## Delete all file except all zip and iso files ##
rm  !(*.zip|*.iso)
 
## You set full path too ##
rm /Users/vivek/!(*.zip|*.iso|*.mp3)
 
## Pass options ##
rm [options]  !(*.zip|*.iso)
rm -v  !(*.zip|*.iso)
rm -f  !(*.zip|*.iso)
rm -v -i  !(*.php)
 

Finally, turn off extglob option:

 
shopt -u extglob
 

Method #2: Using bash GLOBIGNORE variable to remove all files except specific ones

From the bash(1) page:

A colon-separated list of patterns defining the set of filenames to be ignored by pathname expansion. If a filename matched by a pathname expansion pattern also matches one of the patterns in GLOBIGNORE, it is removed from the list of matches.

To delete all files except zip and iso files, set GLOBIGNORE as follows:

## only works with BASH ##
cd ~/Downloads/
GLOBIGNORE=*.zip:*.iso
rm -v *
unset GLOBIGNORE
 

Method #3: Find command to rm all files except zip and iso files

If you are using tcsh/csh/sh/ksh or any other shell, try the following find command syntax on a Unix-like system to delete files:

 
find /dir/ -type f -not -name 'PATTERN' -delete
 

OR

## deals with weird file names using xargs ##
find /dir/ -type f -not -name 'PATTERN' -print0 | xargs -0 -I {} rm {}
find /dir/ -type f -not -name 'PATTERN' -print0 | xargs -0 -I {} rm [options] {}
 

To delete all files except php files in ~/sources/ directory, type:

 
find ~/sources/ -type f -not -name '*.php' -delete
 

OR

 
find ~/sources/ -type f -not -name '*.php' -print0 | xargs -0 -I {} rm -v {}
 

The syntax to delete all files except *.zip and *.iso is as follows:

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

For more information see bash command man page and find command man page.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 13 comments… read them below or add one }

1 Jeff June 3, 2014 at 9:16 pm

For your find command, instead of piping the results through xargs, you could also use the ‘-exec’ option, like so:

find ~/sources/ -type f -not -name ‘*.php’ -exec rm {} \;

Its just another option.

Reply

2 Nix Craft June 4, 2014 at 6:59 am

Yup, thanks for the post :)

Reply

3 David June 9, 2014 at 6:46 pm

It’s another option that orders of magnitude slower than xargs, because it calls one rm command per filename. There’s a much less well-known option than both of those as it handles weird filenames find and can execute a command with multiple filenames just as xargs does. Just replace the semicolon in the find command with a plus sign (which also eliminates the need for the ugly backslash):

find ~/sources -type f -not -name ‘*.php’ -exec rm +

Of course for rm, the -delete option is cleaner, but the -exec command + thing can be used with other commands too.

Reply

4 Starlyte July 24, 2014 at 8:48 pm

I usually do MV to the Trash to be on the safe side, then rm the Trash file.
I’ve had accidents doing a rapid rm.

Reply

5 Starlyte July 24, 2014 at 8:49 pm

Well, in fact it’s mv, in miniscules.

Reply

6 Chris F.A. Johnson June 4, 2014 at 12:49 am

There is no need to export GLOBIGNORE.

Reply

7 Nix Craft June 4, 2014 at 6:59 am

Ah, yes. Thanks for the heads up.

Reply

8 John June 4, 2014 at 12:55 pm

Also, GLOBIGNORE works with other unix/bash/shell commands:
ls *
mv * /dest

So you may need to unset GLOBIGNORE, as soon as you delete files :P

Reply

9 Chris F.A. Johnson June 10, 2014 at 3:37 am

GLOBIGNORE has nothing to do with those or any other commands; it affects how the shell expands wildcards, The commands (any commands) only see the result of the expansion.

Reply

10 Stephan Hughson June 4, 2014 at 1:38 am

Lazy option:

Move the files you want to keep somewhere else.

Delete everything that is left!

Move the files you wanted to keep back again.

It’s simple so little chance of making a mistake but the options above are pretty cool :-)

Reply

11 Scott Carlson June 4, 2014 at 12:13 pm

I’ve never seen extglob before. Nice.

I’d typically do something like…

rm $(ls | grep -v -e iso$ -e zip$)

Reply

12 Mark Kenny June 4, 2014 at 6:11 pm

Great script.

Small error in very last code snippet, should be ‘*zip’ instead of ‘*php’

Reply

13 Nix Craft June 5, 2014 at 6:28 am

Thanks for the heads up!

Reply

Leave a Comment

Tagged as: , ,

Previous Faq:

Next Faq: