Linux: Bash Delete All Files In Directory Except Few

last updated in Categories , ,

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:[donotprint][/donotprint]

  • * - 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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

17 comment

  1. 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.

    1. 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.

    2. 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.

    1. 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

      1. 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.

  2. 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 :-)

  3. I’ve never seen extglob before. Nice.

    I’d typically do something like…

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

  4. Great script.

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

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

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

    1. 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:)))

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

    Still, have a question? Get help on our forum!