Linux – Remove all file in a folder minus one zipped file

bashlinux

How can I remove all files in one folder minus one file (.zip) from the command line? I am using bash to ssh to my server where I want to do this. I know I could use rm -rf * being in that folder, but I need to keep the zipped file as that contains all the new files to replace the others. How can I do this from the command-line?

Best Answer

$ shopt -s extglob
$ rm -fr !(*.zip)

info "(bash) Pattern Matching"

   If the extglob shell option is enabled using the shopt builtin, several extended pattern matching  opera‐
   tors are recognized.  In the following description, a pattern-list is a list of one or more patterns sep‐
   arated by a |.  Composite patterns may be formed using one or more of the following sub-patterns:

          ?(pattern-list)
                 Matches zero or one occurrence of the given patterns
          *(pattern-list)
                 Matches zero or more occurrences of the given patterns
          +(pattern-list)
                 Matches one or more occurrences of the given patterns
          @(pattern-list)
                 Matches one of the given patterns
          !(pattern-list)
                 Matches anything except one of the given patterns