Operating System - Linux
1752740 Members
5882 Online
108789 Solutions
New Discussion юеВ

script for deleting log files in Linux

 
yuva_mca
Occasional Advisor

script for deleting log files in Linux

How to write the shell scripts to delete one month old log files.
Please let me know the script with example.

Thanks
Yuva
9 REPLIES 9
RAC_1
Honored Contributor

Re: script for deleting log files in Linux

Many ways to do it. man find

find /dir_where_files_reside -type f -mtime +30 -exec rm {} \;

find /dir_where_files_reside -type d -mtime +30 -exec rm {} \;


Do not use -exec and rm first. Look at files and then delete.

find /dir_where_files_reside -type f -mtime +30 -exec ll -d {} \+
There is no substitute to HARDWORK
Muthukumar_5
Honored Contributor

Re: script for deleting log files in Linux

You can find those files with find command using -mtime (modified time) +30 (30 days or old).

find -type f -mtime +30 | xargs rm -f

You can create a file with that time stamp and find with -newer option in find command.

# touch [[CC]YY]MMDDhhmm[.SS] file1

Change the time + date settings based on your requirement.

# find -type f -newer file1 | xargs rm -f

hth.
Easy to suggest when don't know about the problem!
yuva_mca
Occasional Advisor

Re: script for deleting log files in Linux

Thanks for your effort. Now I am clear to find the files & directory and delete the same.

Thanks
Yuva
Ivan Ferreira
Honored Contributor

Re: script for deleting log files in Linux

You can also use the tmpwatch service included with linux to delete files not accesed in a specific period of time.

See man tmpwatch, the script is located at /etc/cron.daily/tmpwatch.

The good thing with this is that you can tell to tmpwatch to not delete files if are in use.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Ralf Hildebrandt
Valued Contributor

Re: script for deleting log files in Linux

The correct solution is to be found in "man logrotate".
Postfix/BIND/Security/IDS/Scanner, you name it...
raj_sudhan
New Member

Re: script for deleting log files in Linux

on the same lines, could you guys please let me know how could mtime be used to delete files that are say 3 hrs old.

thanks,
Raj
Rick Garland
Honored Contributor

Re: script for deleting log files in Linux

Can utilize the logrotate utility.

In the logrotate.conf file you make make specifications based on your wants.
Steven E. Protter
Exalted Contributor

Re: script for deleting log files in Linux

Shalom Yuva,

chkconfig logrotated on
service logroated on

Configuration files are in /etc/logrotate.d

Pretty well documented.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Joel Jos
New Member

Re: script for deleting log files in Linux

I had seen the above given code. its helpful for me. i have one more doubt.

I have to navigate to each directory from root and display files. Then i have to check delete the files older than 3 months. i can do it with the help of above mentioned code. But i have one more constraint. I have to keep at least 5 later versions though its older than 3 months.

can anyone help me in this regard?

Thanks in advance.