linux下删除7天前日志的代码(php+shell)

PHP版本:
<div class="codetitle"><a style="CURSOR: pointer" data="84006" class="copybut" id="copybut84006" onclick="doCopy('code84006')"> 代码如下:

<div class="codebody" id="code84006">
/
删除7天前的日志
@param $logPath
/
function del7daysAGolog($logPath) {
if(empty($logPath))return;
$handle = opendir($logPath);
while(($file = readdir($handle)) !== false){
$pos = strpos($file,'.log');
if ($pos !== false && (strtotime("-1 week") > fileatime($logPath . $file))) {
unlink($logPath . $file);
}
}
}
shell 版本
<div class="codetitle"><a style="CURSOR: pointer" data="75720" class="copybut" id="copybut75720" onclick="doCopy('code75720')"> 代码如下:
<div class="codebody" id="code75720">
#!/bin/sh
function del7daysAGolog (){
for file in $(ls $1)
do
if [ "${file##
.}" = "log" ]
then
ctime=$(stat $1/$file -c "%y")
ctimeU=$(date -d "$ctime" +%s)
Now=$(date +%s)
SevendaysAgo=$(($Now - 36000 $Days))
if [ $SevendaysAgo -gt $ctimeU ]
then
$(rm $file)#此处删除文件
fi
else
echo ""
fi
done
}
Days=7
Path="/var/www/
/log"
del7daysAGolog $Path $Days
shell 版本比较麻烦 关键我linux转换不熟悉

删除日志

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...