【问题标题】:Delete everything but a specific folder删除除特定文件夹以外的所有内容
【发布时间】:2015-01-06 23:16:09
【问题描述】:

我想知道我会将哪些选项传递给rm -rf 以删除除.git/ 文件夹之外的所有文件夹。我正在阅读手册页,但我对您可以传递的选项感到困惑。如果我想删除除 .git/ 之外的所有内容,那么命令是什么?

【问题讨论】:

    标签: terminal rm


    【解决方案1】:

    一种可能的解决方案是使用find 查找目录中不是 .git 然后rm 它们的所有项目:

    > find . -mindepth 1 -maxdepth 1 ! -name .git -print0| xargs -0 rm -rf;
    

    【讨论】:

      【解决方案2】:

      您可以使用 bash GLOBIGNORE 变量来删除除特定文件之外的所有文件

      来自bash(1) 页面:

      A colon-separated list of patterns defining the set of filenames to be ignored
      by pathname expansion. If a filename matched by a pathname expansion pattern
      also matches one of the patterns in GLOBIGNORE, it is removed from the list
      of matches.
      

      要删除除 zip 和 iso 文件之外的所有文件,请按如下方式设置 GLOBIGNORE:

      GLOBIGNORE=*.git
      rm -rf *
      unset GLOBIGNORE
      

      PS。仅适用于 BASH

      【讨论】:

        【解决方案3】:

        使用 bash :

        shopt -s extglob
        rm -rf !(.git)
        

        在运行此命令之前,请注意在好目录中。

        查看http://mywiki.wooledge.org/glob#extglob

        【讨论】:

        • 这是最新版本的 bash 的功能吗?我无法让它在 GNU bash, version 4.1.16(8)-release (x86_64-unknown-cygwin) 中工作。
        • 哦,我明白了。您需要设置extglob 选项(使用shopt -s extglob 设置)才能正常工作。
        猜你喜欢
        • 2012-12-15
        • 2016-01-19
        • 2012-09-19
        • 2014-04-01
        • 2014-07-25
        • 1970-01-01
        • 2021-06-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多