Tuesday, January 22, 2008

Unix: Compress or delete files older then X days

Problem:
I have some logs in some directories that i need to back up if the file is at least 12 days old, if the file is older then 24 days, i need to delete it

Solution:

to compress:
find <path within you're looking> -xdev -mtime +<number of days> -exec /usr/bin/compress {} \;

to delete:
find <path within you're looking> -xdev -mtime +<number of days> -exec /usr/bin/rm -f {} \;


Note: This was tested on Solaris 8

Examples:

find /usr/local/apache/logs/ -xdev -name "sar.*" -mtime +24 -exec /usr/bin/rm -f {} \;
find /usr/local/apache/logs/ -xdev -name "sar.*" -mtime +12 -exec /usr/bin/compress {} \;

No comments:

Post a Comment