保留目录下最新的几个文件,其他删除

#!/bin/bash

#保留文件数
ReservedNum=5
FileDir=/data/bak
date=$(date "+%Y%m%d-%H%M%S")

FileNum=$(ls -l $FileDir|grep ^- |wc -l)

while(( $FileNum > $ReservedNum))
do
OldFile=$(ls -rt $FileDir| head -1)
echo $date "Delete File:"$OldFile
rm -rf $FileDir/$OldFile
let "FileNum--"
done 

原文链接:https://blog.csdn.net/liyyzz33/article/details/84571044

猜你喜欢

转载自www.cnblogs.com/xiaoyou2018/p/11750507.html