The different extended pattern matching operators are listed below, where pattern-list is a list containing one or more filenames, separated using the
|
character:
*(pattern-list)
– matches zero or more occurrences of the specified patterns?(pattern-list)
– matches zero or one occurrence of the specified patterns- +(pattern-list) – matches one or more occurrences of the specified patterns
@(pattern-list)
– matches one of the specified patterns!(pattern-list)
– matches anything except one of the given patternsTo use them, enable the extglob shell option as follows:
# shopt -s extglob1. To delete all files in a directory except filename, type the command below:
$ rm -v !("filename")2. To delete all files with the exception of filename1 and filename2:
$ rm -v !("filename1"|"filename2")Source: 3 Ways to Delete All Files in a Directory Except One or Few Files with Extensions