3 Ways to Delete All Files in a Directory Except One or Few Files with Extensions

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

12 Responses

  1. Bryan says:

    If the directory contains sub-directories, which may or may not contain file/s of a particular extension, how to delete all other files recursively from the sub-directories as well, without deleting our file/s of particular extension.

  2. Anand says:

    what is shopt -s extglob? and why can’t i able to delete without using this command like rm -v !(“*.gz”).

  3. Javier Huaman Martinez says:

    Perfect information.

  4. Dave Lane says:

    Nice tips, thanks for that, my only concern is that, if you get anything wrong, you’ll delete the files you don’t want to delete… I would’ve thought it’d better to move the file you want to save elsewhere temporarily, and then remove all the files in the directory and replace the one(s) you want to keep…

    • Aaron Kili says:


      Welcome, and nice tip as well. Things may go wrong especially when you type a wrong command, as you have mentioned, it is always a good practice to do a backup before removing files.

      • Greg P says:

        There probably should be some sort of saying that “two negative commands does not make for a positive result”, especially when you don’t have some undo method. So removing except for some kinds of files just seems a bit scary.

        Backing up before you do this seems like extra work. At any rate, I typically will try to find a nondestructive operation like ls to make sure something like this does what I want before I would even consider using rm.

        In particular, a nondestructive command that specifically lists what you will delete with rm the best. Maybe using mv first would be a good idea.

        • Aaron Kili says:


          Well said and its a good tip as you have explained here, however, cleaning up a directory by removing all files except a few would probably be a user’s intention. And before doing any removal of files, one must know why he/she is doing so, whether to remove old files of a particular type or just create more space on disk and so on.

          Lastly, the aim of the article is to provide a solution for a scenario where you can clear all files in a directory but need to leave those that end with a given extension.

  5. djf says:

    Obscure & good!!

    find I have used – but the other two are new – and until now unknown to me. But they are in my linux database thanks to you. ;-)

  6. sedlav says:

    Curiously all tools (rm, find, shopt) and mainly concepts (GLOBIGNORE, wildcard) mentioned here comes from GNU project but you give all credit to the Linux project I think this is a not correct position about the GNU Project which deserve as credit as Linux Project.

    • Aaron Kili says:


      This is a important concern in relation to support for F.O.S.S in general, we shall do as you have said in every future article. However, we have always given credit to both the Linux and GNU projects in the past.

Got something to say? Join the discussion.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.