How to delete all but one of the files in a directory

delete Linux files

Sometimes you need remove almost all files from a directory, but you want to keep one or some of them. When there are large numbers of them, going one by one is a tedious task. It is not the best option, there are ways to make the work in Linux much easier and that you can eliminate all the ones you need at once.

For example, you may want to remove only those that start with a certain name, or those that have a specific extension, etc. All that is possibleIn fact, on other occasions I have already shown similar tutorials in LxA. Here you can follow the tutorial step by step and in a simple way to be able to delete all those files you want, except what you want to save.

And the best thing is that you will not need to install any program, it can be done easily with commands like rm and find. That is, programs that are already pre-installed on any Linux distro. And of course, the method will be based on finding patterns and using those matches to remove only what you want.

Well, in order to eliminate there several alternatives, What are they…

Remove files from a directory with rm

Well, in order to use the rm command To eliminate what you feel like, you have to know before some ways to identify patterns:

  • * (list of patterns) - matches zero or more occurrences of the specified patterns
  • ? (list of patterns) - matches zero or one occurrence of the specified patterns
  • + (pattern list) - matches one or more occurrences of the specified patterns
  • @ (pattern list) - matches one of the specified patterns
  • ! (pattern list) - matches anything except one of the given patterns

For, enable extglob In order to use them, you have to first execute the following command:

<br data-mce-bogus="1">

shopt -s extglob<br data-mce-bogus="1">

 

 

eye! I don't specify it, but it is assumed that you have permissions to do these operations, and that you are inside that directory when you execute the rm command. Be careful with this, because if you run it in another path, you may end up deleting files that you do not want. That is, before executing these commands, make sure that you have entered the directory you want with cd.

Now you can use rm to remove whatever you want. For example, delete all files from a directory except those that match the name «Lxa»:

rm -v !("lxa")

You can also specify two or more names that you don't want to delete. For example, to avoid removing "lxa" and "desdelinux":

 rm -v !("lxa"|"desdelinux") 

You can delete all files, minus those with extension .mp3. For example:

 rm -v !(*.mp3) 

At the end, you can go back to disable extglob:

 shopt -u extglob 

Remove files from a directory with find

Another alternative to rm is use find to remove whatever you fancy. You can use a pipe and xargs with rm, or use the -delete option to find. That is, the generic syntax would be:

find /directory/ -type f -not -name 'PATRÓN' -delete
find /directory/ -type f -not -name 'PATRÓN' -print0 | xargs -0 -I {} rm [opciones] {}

For example, imagine you want delete all files in a directory except those with the extension .jpg, you could use one of these two commands, since they both get the same result:

find . -type f -not -name '*.jpg'-delete

find . -type f -not -name '*.jpg' -print0 | xargs -0 -I {} rm -v {}

Instead, if you wanted add some extra pattern, you could too. For example, suppose you don't want to remove either the .pdf or .odt from a directory:

find . -type f -not \(-name '*pdf' -or -name '*odt' \) -delete

Of course, you could do the same with | and xargs as in the previous example. By the way, we have used -not to deny, but you can remove that to make it positive, that is, to remove the matching patterns and not exclude them.

Delete files from a directory using the GLOBIGNORE variable

Finally, there is Another alternative to find and rm, and it's using an environment variable to point to the files you want to remove or exclude. For example, imagine you want to delete all the files in a directory called Downloads, saving the .pdf, .mp3 and .mp4 files. In that case, you could do the following:

cd Descargas
GLOBIGNORE=*.pdf:*.mp4:.*mp3
rm -v *
unset GLOBIGNORE


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

Be the first to comment

Leave a Comment

Your email address will not be published.

*

*

  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.