Script to compress and delete the older logs

Hello Guys,

Can you please help me with a script which zips the older log files(1-2 weeks) and delete them?

I want to run the script manually instead of setting it up in a cron job.

Appreciate your help.

Regards,
Kris

Find command can do this task for you.I request you to check man pages for more info.

find <<directory-path>> -type f -mtime +N -exec rm {} \;

N-No of days older file which you want to delete.

Hello Kris.gv,

Could you please let me know here if you are going to delete the files older than 1/2 weeks then what is the need to zip them? Could you please explain your requirement bit clearer on same.

Thanks,
R. Singh

Hi R.Singh,

I would like to zip the files which are two weeks older from yesterday and remove the files which are 1 week older from yesterday.

Regards,
Kris

Hello Kris.gv,

So let's say you are running script once in a week either manually or by a scheduled method then most probably following may happen.
1st week ----> put all files more than 2 weeks to zip and remove all files which are older than 1 week.
2nd week ----> (As there shouldn't be any file older than 1 week) so it will not able to zip any files only it will delete files older than 1 week.
3rd week ----> Similarly as mentioned on second week as no file is ever reaching to modify date more than 2nd week so nothing will be zipped only 1 week older files will be deleted again.

So if you see only 1st week is effective here for zipping the files older than 2 weeks later each week it should only delete the files, so this applies if both zipping the files and deleting the files happens in same directory. So my question is, is it the same path or different paths where you want to perform these actions? Also if you have more conditions in this request kindly do mention them too.

Thanks,
R. Singh

Hi R.Singh,

The script I wanted to execute is likely once a month manually, and also I want to set the script permissions to developers so they can excute when needed(this part I can take care).

The directory path is same .

Regards,
Kris

Hello kriss.gv,

If you want to run in commands then following you could use.

TEST1##find -mindepth 1 -type f -mtime +15 -print
Actual 1st:find -mindepth 1 -type f -mtime +15 -exec gzip {} \+
  
TEST2##find -mindepth 1 -type f -mtime +8 -print
Actual 2nd:find -mindepth 1 -type f -mtime +8 -delete

So wanted to mention here first kindly run the hashed commands to check if the results are coming up to the mark and once you are satisfied then only run the commands written after Actual.
Also always run the 2 weeks before command(Actual 1st) firstly else if you run command for 1 week(Actual 2nd) before week files then it will delete all the files before 2 weeks before files also comes under same category too.

Kindly do let me know if this helps you.

NOTE: You could mention path also for command else it will take the current directory where you are executing the command and you could increase/decrease value of mindepth as per your requirement too.

Thanks,
R. Singh