gpt4 book ai didi

linux - 无法删除超过三天的目录

转载 作者:太空宇宙 更新时间:2023-11-04 04:45:58 24 4
gpt4 key购买 nike

我需要删除所有超过三天的子目录。下面的代码应该可以工作,但事实并非如此......

for i in `find ~/web/smsng/ -maxdepth 1 -type d -mtime +3 -print`; do echo -e "Deleting directory $i";rm -rf $i; done

完整的ls-l目录列表:

(uiserver):u83749873:~/models/ndfd > ls -l
total 1536
drwx---r-x 2 u83749873 ftpusers 12288 Apr 8 12:41 2016040816
drwx---r-x 2 u83749873 ftpusers 12288 Apr 8 13:41 2016040817
drwx---r-x 2 u83749873 ftpusers 12288 Apr 8 14:40 2016040818
drwx---r-x 2 u83749873 ftpusers 12288 Apr 8 15:41 2016040819
drwx---r-x 2 u83749873 ftpusers 12288 Apr 9 00:41 2016040904
drwx---r-x 2 u83749873 ftpusers 12288 Apr 9 01:41 2016040905
drwx---r-x 2 u83749873 ftpusers 12288 Apr 9 02:41 2016040906
drwx---r-x 2 u83749873 ftpusers 12288 Apr 10 03:41 2016040907
drwx---r-x 2 u83749873 ftpusers 12288 Apr 10 04:41 2016040907
drwx---r-x 2 u83749873 ftpusers 12288 Apr 11 07:41 2016040907

最佳答案

-mtime +3更改为-mtime +2:

for i in `find ~/web/smsng/ -maxdepth 1 -type d -mtime +2 -print`; do
echo -e "Deleting directory $i"
rm -rf $i
done

根据 find(1) 手册页:

-mtime n

File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.

其他改进

您可以对脚本进行一些改进,以提高处理任何可能的文件名的能力。

目前,如果出现以下情况,您的脚本将无法工作:

  • 任何目录中都包含空格、制表符或换行符
  • $i 变量以连字符 (-) 开头

如果您像这样编写脚本,则可以解决这两个问题:

find ~/web/smsng/ \
-maxdepth 1 -type d -mtime +2 \
-exec echo 'Deleting directory {}' \; \
-exec rm -rf -- {} \;

因为 shell 永远不会将其解释为参数列表,所以永远不会发生空格分割。因为它是通过选项终止 -- 传递给 rm 的,所以文件名可以以连字符开头,并且不会被解释为 rm 的标志。

关于linux - 无法删除超过三天的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36579081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com