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/
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/
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:
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.
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.
- Prevent Files And Folders From Accidental Deletion Or Modification In Linux
- An Easy Way to Protect Files From Accidental Deletion In Linux
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!