Script to compress and delete the older logs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to compress and delete the older logs
# 1  
Old 12-27-2016
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
# 2  
Old 12-27-2016
Find command can do this task for you.I request you to check man pages for more info.

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

N-No of days older file which you want to delete.
# 3  
Old 12-27-2016
Quote:
Originally Posted by kriss.gv
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
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
# 4  
Old 12-27-2016
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
# 5  
Old 12-27-2016
Quote:
Originally Posted by kriss.gv
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
# 6  
Old 12-27-2016
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
# 7  
Old 12-27-2016
Hello kriss.gv,

If you want to run in commands then following you could use.
Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compress folders older than x days

hello everyone. in /opt/abc every night there is a new folder created. in that folder there is aseries of files created for that day. i would like to run a script every Sunday night at 02:00 to compress each file separately (preserving its name) who is older than 2 days. i have found this... (2 Replies)
Discussion started by: atux
2 Replies

2. UNIX for Dummies Questions & Answers

How to compress the directories which is older than 7 days?

Hi all, how to compress the directories which is older 7 days. If any one knows please help me this is urgent. Thanks in advance (3 Replies)
Discussion started by: rameshpagadala
3 Replies

3. Shell Programming and Scripting

Script to delete files in a folder older than 2 days

hi i need a script to delete the files older than 2 days... if my input is say in a folder versions A_14122012.txt A_15122012.txt A_16122012.txt A_17122012.txt i want my output to be A_16122012.txt A_17122012.txt thanks in advance hemanth saikumar. (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

4. Red Hat

Need Script to ZIP/SAVE & then DELETE Log file & DELETE ZIPS older than 12 months

ENVIROMENT Linux: Fedora Core release 1 (Yarrow) iPlanet: iPlanet-WebServer-Enterprise/6.0SP1 Log Path: /usr/iplanet/servers/https-company/logs I have iPlanet log rotation enabled rotating files on a daily basis. The rotated logs are NOT compressed & are taking up too much space. I... (7 Replies)
Discussion started by: zachs
7 Replies

5. Shell Programming and Scripting

Delete logs older than 60 days

i am using HP-UX OS...... delete logs older than 60 days find -mtime +60 | grep -i '.*log' | xargs rm -mtime is nt available in HP-UX, pls tell me other option ? (2 Replies)
Discussion started by: only4satish
2 Replies

6. Shell Programming and Scripting

To delete logs older than 30 days

I want to write a shell script that deletes all log files in a directory that are older than 30 days except for 3 files: I am using the following command: find /tmp/logs -name "*.log" -mtime +30 -exec rm -f {} \;But this command deletes all the log files. How can i modify this script that... (5 Replies)
Discussion started by: mmunir
5 Replies

7. Shell Programming and Scripting

How to tar, compress and remove files older than two days

Hi, I'm Eddy from Belgium and I've the following problem. I try to write a ksh script in AIX to tar, compress and remove the original *.wav files from the directory belgacom_sf_messages older than two days with the following commands. The problem is that I do not find a good combination... (4 Replies)
Discussion started by: edr
4 Replies

8. Shell Programming and Scripting

Script to delete older versions of unique files

I have directory where new sub directories and files being created every few minutes. The directories are like abc_date, def_date, ghi_date. I am looking to keep the latest 2 unique directories and delete everything else. Here is what I have so far This gives me unique names excluding the... (5 Replies)
Discussion started by: zzstore
5 Replies

9. Shell Programming and Scripting

Delete files older than 2 days using shell script in Unix

I'm new to shell script.... can any one help... What is the shell script to delete the files older than 2 days ? (3 Replies)
Discussion started by: satishpabba
3 Replies

10. UNIX for Advanced & Expert Users

how to archive logs older than 5 days & then delete them?

My code is tar -cvf logs.tar `find /usr/openv/logs/512*.log -mtime +2` && find *.log* -mtime +2 -exec rm {} \; this gives me output as: tar: Missing filenames:confused: (1 Reply)
Discussion started by: timus1980
1 Replies
Login or Register to Ask a Question