This page looks best with JavaScript enabled

Remove all files but one with rm

 ·   ·  ☕ 1 min read  ·  ✍️ Sathya · 👀... views

Is there any way to remove all of the files in a directory except for one with a certain filename? For instance, if I had a directory containing the files file1, file2, and dontdelete. Would there be any way to quickly delete file1, and file2 and not dontdelete? I know that I could just do rm file1 file2 but that wouldn’t work for a bunch of files. Also, I’m on Mac OS X if that makes a difference

via command line - Remove all files but one with rm - Super User.

Nice tip.

Share on

Sathyajith Bhat
WRITTEN BY
Sathya
Author, AWS Community Hero and DevOps Specialist

What's on this Page