Linux: Bash Delete All Files In Directory Except Few

Posted on in Categories , , last updated June 5, 2014

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(1) and find(1).

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

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.

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

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

Leave a Comment