无法删除超过三天的目录
Unable to Delete Directories Older Than Three Days
我需要删除所有超过三天的子目录。下面的代码应该可以工作,但不是...
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.
其他改进
您可以对脚本进行一些改进,以提高处理任何可能文件名的能力。
目前,如果出现以下情况,您的脚本将无法运行:
- 任何目录中都有 space、制表符或换行符
$i
变量以连字符 (-
) 开头
如果您像这样编写脚本,则可以解决这两个问题:
find ~/web/smsng/ \
-maxdepth 1 -type d -mtime +2 \
-exec echo 'Deleting directory {}' \; \
-exec rm -rf -- {} \;
因为它从未被 shell 解释为参数列表,所以白色拆分 space 永远不会发生。因为它是通过选项终止 --
传递给 rm
的,所以文件名可以以连字符开头,并且不会被解释为 rm.
的标志
我需要删除所有超过三天的子目录。下面的代码应该可以工作,但不是...
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.
其他改进
您可以对脚本进行一些改进,以提高处理任何可能文件名的能力。
目前,如果出现以下情况,您的脚本将无法运行:
- 任何目录中都有 space、制表符或换行符
$i
变量以连字符 (-
) 开头
如果您像这样编写脚本,则可以解决这两个问题:
find ~/web/smsng/ \
-maxdepth 1 -type d -mtime +2 \
-exec echo 'Deleting directory {}' \; \
-exec rm -rf -- {} \;
因为它从未被 shell 解释为参数列表,所以白色拆分 space 永远不会发生。因为它是通过选项终止 --
传递给 rm
的,所以文件名可以以连字符开头,并且不会被解释为 rm.