Friday, September 16, 2005

Removing old files that contain a phrase using crontab

Removing old files that contain a phrase using crontab
00 4 * * * find /sap_basis/log -name "decrypt_DH9383PS.log*" -mtime +14
-exec sh -c 'grep "No Matches" $1 >/dev/null 2>&1 && rm $1' {} {} \;

This is a tiny shell script (written inline), which runs grep and if
that reports success then runs rm. The doubled {} are to handle case
that $0 is assigned the first arguement, and $1 the second one.

Just for fun, a further refinement: grep -l will output file names only,
and each file at most once, obviating the need to cut and sort -u.

find /sap_basis/log -name "decrypt_DH9383PS.log*" -mtime +14 |
xargs grep -l "No Matches" | xargs rm

0 Comments:

Post a Comment

<< Home