Home Command line utilities How To Remove All Files In A Folder Except One Specific File In Linux

How To Remove All Files In A Folder Except One Specific File In Linux

By sk
Published: Last Updated on 15.4K views

Let us say you have 100+ files in a folder. You want to delete all of them except one or few specific files. How would you do it? You can copy the files you wanted to keep, and save them in a different location, and then delete the rest of the files or the entire folder. But wait, I know an easiest way to do this. You can remove all files in a older except one specific file or certain type of files in one go with a single line command. Want to know how? Read on.

Remove All Files In A Folder Except One Specific File

Let us picture the following example. We have a folder called 'test' that contains 10 text files.

$ ls test/

Sample output:

file10.txt file2.txt file4.txt file6.txt file8.txt
file1.txt file3.txt file5.txt file7.txt file9.txt

Now, I want to delete everything in this folder except file10.txt.

There might be many commands to do this. But these are the five commands that I am aware of.

First, go to the test folder:

$ cd test/

And run the following command:

$ rm -f !(file10.txt)

Or, just use:

$ rm !(file10.txt)

The above command will delete all files in the test folder except file10.txt file.

You can also use find command to delete everything but specific one. The following command will delete all files in the current folder (i.e test in our case) except file10.txt.

$ find . ! -name file10.txt -delete

As you see in the above example, the test folder contains same type of files i.e .txt files. What would you do if the folder has different type of files like .mp3, .doc, .pdf etc.? It's also easy to keep particular type of files in a folder and delete everything else.

Let us say our test folder contains three .txt files, three .mp3 files, three .doc files, and one .pdf file.

$ ls test/

Sample output:

total 0
-rw-r--r-- 1 sk users 0 Mar 22 15:51 file10.pdf
-rw-r--r-- 1 sk users 0 Mar 22 15:51 file1.txt
-rw-r--r-- 1 sk users 0 Mar 22 15:51 file2.txt
-rw-r--r-- 1 sk users 0 Mar 22 15:51 file3.txt
-rw-r--r-- 1 sk users 0 Mar 22 15:51 file4.mp3
-rw-r--r-- 1 sk users 0 Mar 22 15:51 file5.mp3
-rw-r--r-- 1 sk users 0 Mar 22 15:51 file6.mp3
-rw-r--r-- 1 sk users 0 Mar 22 15:51 file7.doc
-rw-r--r-- 1 sk users 0 Mar 22 15:51 file8.doc
-rw-r--r-- 1 sk users 0 Mar 22 15:51 file9.doc

As you in the above output, I have four different type of files (pdf, txt, mp3, doc) in the test folder. I'd like to keep the files that has .doc extension and remove everything else. Here is how I can do this:

$ cd test/
$ rm !(*.doc)

Now, let us list the file contents using command:

$ ls

Sample output:

file7.doc  file8.doc  file9.doc

The above command delete everything in the folder except the files that has extension .doc.

Similarly, you can keep two or more particular types of files and remove everything else. Say for example, the following command will keep the files that contains .doc and .mp3 extensions.

$ rm !(*.doc|*.mp3)

Now, you will see the mp3 and doc files are not deleted.

$ ls

Sample output:

file4.mp3  file5.mp3  file6.mp3  file7.doc  file8.doc  file9.doc

These are just ten different type of files. Just image you have hundreds of files. It would be harder to find each file type and delete them manually. This trick will do the job in just one or two seconds.

Please be very careful while using these commands. Double check the directory path before deleting files to avoid accidental deletion of important files.


Suggested read:


Hope this helps. If you find this guide useful, please share it on your social, professional networks and support us.

More good stuffs to come. Stay tuned!

Cheers!

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

2 comments

Rogerio Pereira da Silva March 25, 2017 - 7:52 pm

ls | egrep -v ‘.(doc|mp3|pdf)$’ | xargs -iXX rm -fv XX

Reply
abdul October 28, 2020 - 3:28 pm

hi, using zsh not works

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

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

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More