≡ Menu

Linux: Bash Delete All Files In Directory Except Few

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:

  • * - 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/
rm -v *

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


## 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


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.

Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:

{ 17 comments… add one }
  • Jeff June 3, 2014, 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.

    • Nix Craft June 4, 2014, 6:59 am

      Yup, thanks for the post :)

    • David June 9, 2014, 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.

    • Starlyte July 24, 2014, 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.

      • Starlyte July 24, 2014, 8:49 pm

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

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

    There is no need to export GLOBIGNORE.

    • Nix Craft June 4, 2014, 6:59 am

      Ah, yes. Thanks for the heads up.

    • John June 4, 2014, 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

      • Chris F.A. Johnson June 10, 2014, 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.

  • Stephan Hughson June 4, 2014, 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 :-)

  • Scott Carlson June 4, 2014, 12:13 pm

    I’ve never seen extglob before. Nice.

    I’d typically do something like…

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

  • Mark Kenny June 4, 2014, 6:11 pm

    Great script.

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

  • Deepak December 10, 2014, 10:21 am

    I am getting error on my environments. Is this command Linux OS dependent??

    $ rm -f !(My.properties|My.xml)
    -bash: !: event not found

  • Scott Carlson December 15, 2014, 1:40 am

    @Deepak, you forgot to turn on extglob first.

    shopt -s extglob
    ls !(My.properties|My.xml)

    • ikhmat February 11, 2015, 5:25 am

      What if we don’t turn extglob off after we started it?.. Will it cause any difference/problem for regular command that we run normally. So why Don’t we keep it “ON” all the time?. Sorry I am a starter:)))

  • Ashish June 3, 2015, 5:52 am

    what does ” -type f” do…because even without it, it does the same thing…can anyone explain please?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , ,