To use them, enable the extglob shell option as follows:
# shopt -s extglob
1. 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")
3. The example below shows how to remove all files other than all .zip
files interactively:
$ rm -i !(*.zip)
4. Next, you can delete all files in a directory apart from all .zip
and .odt
files as follows, while displaying what is being done:
$ rm -v !(*.zip|*.odt)
Once you have all the required commands, turn off the extglob shell option like so:
$ shopt -u extglob