Linux – How to Delete All Files in a Directory Except Some

bashlinuxrm

I need to delete all files in a directory, but exclude some of them. For example, in a directory with the files a b c ... z, I need to delete all except for u and p. Is there an easy way to do this?

Best Answer

What I do in those cases is to type

rm *

Then I press Ctrl+X,* to expand * into all visible file names.

Then I can just remove the two files I like to keep from the list and finally execute the command line.

Related Question