【问题标题】:Bash scripting: Deleting the oldest directoryBash 脚本:删除最旧的目录
【发布时间】:2011-04-06 10:48:39
【问题描述】:

我想查找最旧的目录(在目录中),然后将其删除。我正在使用以下内容:

rm -R $(ls -1t | tail -1)

ls -1t | tail -1 确实给了我最旧的目录,问题是它没有删除目录,而且它还列出了文件。

我该如何解决这个问题?

【问题讨论】:

    标签: bash file-io directory delete-directory


    【解决方案1】:
    rm -R $(ls -tl | grep '^d' | tail -1 | cut -d' ' -f8)
    

    【讨论】:

      【解决方案2】:

      这并不漂亮,但它有效:

      rm -R $(ls -lt | grep '^d' | tail -1  | tr " " "\n" | tail -1)
      

      【讨论】:

      • 注意:如果目录名中包含空格则不起作用。
      【解决方案3】:
      rm -R "$(find . -maxdepth 1 -type d -printf '%T@\t%p\n' | sort -r | tail -n 1 | sed 's/[0-9]*\.[0-9]*\t//')"
      

      这也适用于名称包含空格、制表符或以“-”开头的目录。

      【讨论】:

        【解决方案4】:

        
        find directory_name -type d -printf "%TY%Tm%Td%TH%TM%TS %p\n" | sort -nr | tail -1 | cut -d" " -f2 | xargs -n1 echo rm -Rf
        
        如果它产生正确的结果,您应该在 rm 之前删除回声

        【讨论】:

          猜你喜欢
          • 2021-01-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-28
          • 2011-01-28
          • 1970-01-01
          • 2018-03-19
          相关资源
          最近更新 更多