[How To] Delete all but some files in UNIX

While deleting or removing files from orr file system, sometimes we wish to exclude files which satisfy a certain criteria.

The below tutorial explains how to delete all files except the files that have a certain text in their file name.

::syntax::
$ rm !(*pattern_to_exclude*)

The above rm command tells the shell to delete all the files in the current directory except the files that match the pattern inside the parenthesis. The '!' acts as a negation operator.

The below screen-shot illustrates the same. It deletes all the files in the current directory except for the files that contain the string "Dont" in the filename.


remove / delete all files except a few [click on the image to enlarge]