[英]Delete files & folders recursively
I have so much files and folders. 我有太多的文件和文件夹。 They have a syntax like this path
他们有这样的语法
/opt/logs/qnap/[hosta,hostb]/2012/03/12 / opt / logs / qnap / [hosta,hostb] / 2012/03/12
SixMonthAgo=$(date --date='190 day ago' "+%Y/%m/%d/%H") = 2011/06/12/12
Hosts="hosta hostb"
maxDay=181
qnapFolder="/opt/logs/qnap"
for host in $(echo "${Hosts}"); do
maxDayAgo=$(date --date="${maxDay} day ago" "+%Y/%m/%d")
countCharacters=$(echo ${maxDayAgo} | wc -c)
if [ ${countCharacters} -ge 10 ]; then
rm -rf ${qnapFolder}/${host}/${maxDayAgo} #output of this just like 2012/03/12
fi
done
But sometimes i got an error. 但是有时候我会出错。 This soluiton is not working correctly what I want.
此解决方案无法正常运行我想要的。 I want to delete all files and folders before 180 days.
我想在180天之前删除所有文件和文件夹。 How can I do this ?
我怎样才能做到这一点 ? That must delete before all files and directories before 2012/03/23 for example .
例如,必须在2012/03/23之前删除所有文件和目录。
Thanks in advance 提前致谢
The find command is slow? find命令很慢? Are you certain?
你确定吗? The only drawback is that it fails to remove the directory structure.
唯一的缺点是它无法删除目录结构。
find /opt/logs/qnap -type f -mtime +180 -delete
If the directories were indeed created at the same time then maybe removing -type f
could work for you. 如果确实在同一时间创建目录,则删除
-type f
可能对您-type f
。
find /opt/logs/qnap -mtime +180 -delete
find is very fast mind you ... but if you really want to do it with a shell script ... find是一个非常快速的主意...但是如果您真的想使用shell脚本来实现...
CUTOFF=$(date --date='190 day ago' "+%s)
HOSTS="hosta hostb"
DIRECTORY="/opt/logs/qnap"
for HOST in $(ls -1 $DIRECTORY); do
if [ -d $DIRECTORY$HOST ]; then
for DATE in $(ls -1 $DIRECTORY$HOST); do
if [ -d $DIRECTORY$HOST$DATE ]; then
if [ "$CUTOFF" < "$(stat --format="%Y" $DIRECTORY$HOST$DATE)" ]; then
echo "Deleting $DIRECTORY$HOST$DATE"
rm -rf $DIRECTORY$HOST$DATE;
fi
fi
done
fi
I have found the solution just like below. 我已经找到了下面的解决方案。 For instance lets say that the day older than 180 days ago is 2012/04/17 .
例如,假设比180天前的日期是2012/04/17。 That just deletes all directories starting from 2012/04/17 until 2012/04/01.
这只会删除从2012/04/17到2012/04/01的所有目录。 And secondly it deletes every month before 2012/04 .
其次,它会在2012/04之前每月删除一次。 The deletes ones are 2012/03, 2012/02 and 2012/01.
删除的是2012 / 03、2012 / 02和2012/01。
# SixMonthAgo=$(date --date='190 day ago' "+%Y/%m/%d/%H") = 2011/06/12/12
maxDay=104
qnapFolder="/opt/logs/qnap"
for host in $(echo "${Hosts}"); do
maxDayAgo=$(date --date="${maxDay} day ago" "+%Y/%m/%d")
countCharacters=$(echo ${maxDayAgo} | wc -c)
if [ ${countCharacters} -ge 10 ]; then
year=$(echo ${maxDayAgo} |cut -d '/' -f 1)
month=$(echo ${maxDayAgo} |cut -d '/' -f 2) #2012/06
day=$(echo ${maxDayAgo} |cut -d '/' -f 3) #2012/06/04
minday=00
minmonth=00
until [ $month -le $minmonth ]
do
until [ $day -le $minday ]
do
rm -rf ${qnapFolder}/${host}/2012/$month/$day
day=$(printf "%02d" $(expr $day - 01))
done
month=$(printf "%02d" $(expr $month - 01))
rm -rf ${qnapFolder}/${host}/2012/$month
done
fi
done
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.