1. Write a command for To Delete all files except some files & Delete some files except all files ?
Delete Files Using Extended Pattern Matching Operators
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 patterns
To use them, enable the extglob shell option as follows:
# shopt -s extglob
1. Remove all files except one file ?
Command : enable extglob shell option | Shopt -s extglob |
Remove except one file | rm -v !(“one”) |
2. Remove all files except more than one file ?
command | rm -v !(“one”|”three”) |
3. Remove one or more files and keep everything ?
command | rm -v @(“one”|”three”) |
4. Remove all other files by keeping .zip files ?
command | rm -v !(*.zip) |
Once you have all the required commands, turn off the extglob shell option like so:
$ shopt -u extglob