Shell win32删除最旧的目录(递归)

时间:2016-07-14 07:29:25

标签: bash shell recursion directory

我需要从特定路径中删除最旧的文件夹(包括其内容)。例如。如果有超过10个目录,删除最旧的目录,直到你低于8个目录。日志将显示前/后+文件系统之前/之后以及删除了dirs的目录数。 提前谢谢!

2 个答案:

答案 0 :(得分:0)

您应首先在备份目录

上对此进行测试
#!/bin/bash
DIRCOUNT="$(find . -type d -printf x | wc -c)"
if [ "$DIRCOUNT" -gt 10 ]; then
    ls -A1td */ | tail -n -8 | xargs rm -r
fi

答案 1 :(得分:0)

如果我不误解你的意图,下面就是你的回答

  

#! / usr / bin / env bash

     

DIRCOUNT =" $(找。-maxdepth 1-type d -printf x | wc -c)"

     回声"现在你有$ DIRCOUNT dirs"

     

[[" $ DIRCOUNT" -gt 10]]&& ls -A1td * / | tail -n $((DIRCOUNT-8))| xargs rm -r&& echo"现在你有8个dirs"