原文链接:https://blog.csdn.net/beikejinmiao/article/details/18702533

还有一个

tmpwatch 命令

 

一下是链接文章内的脚本文件

#!/bin/bash
##path="/root"
path=$1

if [ ! -d $path ]; then ##判断目录是否存在
echo "Directory '$path' does not exist Or '$path' is not a directory!"
exit 0
fi

if [ ! -x $path -o ! -r $path ]; then ##验证用户对目录是否有足够权限
echo "You have no right to read the directory '$path'!"
exit 0
fi

declare -i dayStr=86400 #一天时间的秒数
declare -i day=7
interval=$(($dayStr*$day)) #时间间隔

for file in $path/* ##遍历目录
do
if [ -f $file ]; then
atime=`stat -c %X $file` ##文件最近一次的访问时间
dtime=`date +%s` ##系统时间
if [ $[ $dtime - $atime ] -gt $interval ]; then
rm -f $file
fi
fi
done
————————————————
版权声明:本文为CSDN博主「beikejinmiao」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/beikejinmiao/article/details/18702533