PHP version:
Copy codeThe Code is as follows :/**
* Delete logs 7 days ago
* @ 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 versionCopy codeThe Code is as follows :#! /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) # delete an object here
Fi
Else
Echo ""
Fi
Done
}
Days = 7
Path = "/var/www/***/log"
Del7daysAgoLog $ Path $ Days
The shell version is troublesome. I am not familiar with linux conversion.