[Linux] 脚本结合crontab定时清理文件

1.clearFolderList中填写需要扫描的路径,clearFiles.sh,二者在相同路径下

clearFiles.sh内容如下

#!/bin/bash

#2880min=48h
HISTORY_TIME=2880

pathlist=`cat /srv/clearFolderList`
#echo $pathlist
for FILE_PATH in $pathlist
do
  find ${FILE_PATH} -type f -mmin +${HISTORY_TIME} | xargs rm -f 2>/dev/null
  find ${FILE_PATH} -type d -mmin +${HISTORY_TIME} | grep -v ${FILE_PATH}'$' | xargs rm -rf 2>/dev/null
done

clearFolderList内容如下

/logs/user1
/logs/user2
/pm/cpu/user1
/pm/cpu/user2
/home/users_statistics

2.使用定时任务执行 clearFiles.sh,执行crontab -e,追加以下行(每小时第15分钟执行)

00 15 * * * /srv/clearFiles.sh

发布了89 篇原创文章 · 获赞 1 · 访问量 4831

猜你喜欢

转载自blog.csdn.net/wy_hhxx/article/details/103804413