SAGARFIVE

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:

  1. *(pattern-list) – matches zero or more occurrences of the specified patterns
  2. ?(pattern-list) – matches zero or one occurrence of the specified patterns
  3. +(pattern-list) – matches one or more occurrences of the specified patterns
  4. @(pattern-list) – matches one of the specified patterns
  5. !(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 filerm -v !(“one”)

2. Remove all files except more than one file ?

commandrm -v !(“one”|”three”)

3. Remove one or more files and keep everything ?

commandrm -v @(“one”|”three”)

4. Remove all other files by keeping .zip files ?

commandrm -v !(*.zip)

Once you have all the required commands, turn off the extglob shell option like so:

$ shopt -u extglob

Leave a Reply

Your email address will not be published. Required fields are marked *