1. 程式人生 > >rm用法之 刪除除了某個檔案之外的所有檔案, extglob enable

rm用法之 刪除除了某個檔案之外的所有檔案, extglob enable

因為目錄下有很多檔案,而且我欲排除的檔案可能正在被使用,所以不能mv。

最後找到這樣的命令:

Linux下除了某個檔案外的其他檔案全部刪除,最簡單的方法是

# shopt -s extglob

# rm -fr !(file1)

man bash
     If the extglob shell option is enabled using the shopt builtin, several extended pattern matching operators are recognized.  In the follow-
       ing description, a pattern-list is a list of one or more patterns separated 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

更多用法可見: