Globs help you: how to delete all files except one

Nautilus on Ubuntu

The globs and pipes of Unix are very beneficial in many cases when we work from the console. You have all used the pipes or used the famous characters like the wildcard * to substitute for names, etc. Well, surely they have also encountered the problem of wanting to delete all the files in a directory except one or more of them. If there are few it is not a problem, it can be done easily even from the graphical mode.

But if there are many and we are working from the console things get a little more complicated. But that's just because we don't have resources, since there are simple options that we can exploit to help us transform complicated tasks into something very simple. Well, in this simple tutorial we explain how to do that just using a simple symbol ... Easy, right? Well, let's go to practice:

First of all, I'm going to imagine that we have a directory called Photos in which we want to delete all the .jpeg image files that are inside. Let's imagine there are 100 photos. And that we want to delete all except one of them called nat7.jpg. The first step is to go to that directory with the help of the cd command. For example:

cd /home/Fotos

Then we will use the rm command as we often do when we want delete something, but we will indicate that we want to erase everything except that photo that we want to keep, for this we will use the character! such that:

rm -f !(nat7.jpg)

With that we are denying the deletion to rm so that it deletes everything except the file named nat7.jpg. If you go to the directory and do a listing of the content, you will be able to verify that everything except the desired photo has been deleted:

ls /home/Fotos

Now you can play with this glob to do other things, such as deleting all files in a directory except MP3 files with! (*. mp3) or also saving various files or formats from deletion, such as! (nat7.jpg | isa5.jpg) or with! (*. txt | * .mp3). The possibilities are many ... This is Unix, this is Linux!


Leave a Comment

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

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Gregory ros said

    There is not a day that we do not learn something! Although I have been messing with PCs for about 30 years and almost 20 with Linux I am not very given to the console, I admit that it is tremendously useful, but I have always been lazy when it comes to memorizing commands and celebrate the move to graphical mode. I did not know this option of the globs at all and although it was not difficult for me to survive without it, it must be granted that it looks super useful and practical, in addition, easy to understand and apply. I take good note.
    Thank you for a contribution that I do not doubt will be very useful.

  2.   9acca9 said

    Hey.
    Thank you very much but…
    rm -f! (*. exe)
    bash:!: event not found

    1.    Isaac PE said

      Hello,

      Try activating this:

      shopt -s extglob; set -H

      And then try again:

      rm -f! (*. exe)

      All the best! I hope it works for you. And thanks for following us ...

  3.   Miguel said

    Hello, I have a question, I have not been able to save two files at the same time because when I try, everything is deleted, but when I do it with only one, I can. what could it be? How to solve it?