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

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. Use our Linode referral link if you plan to buy VPS (it starts at only $10/month).
  4. Support us via PayPal donate - Make a Donation
  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...

11 Responses

  1. Anand says:

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

  2. Javier Huaman Martinez says:

    Perfect information.
    Thanks

  3. 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:

      @Dave

      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:

          @Greg

          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.

  4. 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. ;-)

    • Aaron Kili says:

      @djf

      Welcome and thanks for sharing your thoughts with us. We are always delighted to help our followers learn something new in Linux, as we also hope to learn from them.

  5. 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:

      @sedlav

      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 *

Join Over 300K+ Linux Users
  1. 177,942
  2. 8,310
  3. 37,548

Are you subscribed?