Operating System - HP-UX
1833847 Members
2310 Online
110063 Solutions
New Discussion

Unix shell script for bash shell #!bin/sh to delete the log files after 3 days.

 
Sandy_2
New Member

Unix shell script for bash shell #!bin/sh to delete the log files after 3 days.

Please someone help me to write the Unix shell script for bash shell #!bin/sh to delete the log files after 5 days.
files are in this format xxxxxx-010317-105729-02-17215.log
I am beginner and don't have much idea about scripting.
Please help me

Thanks in advance
Sandy
8 REPLIES 8
Andreas Voss
Honored Contributor

Re: Unix shell script for bash shell #!bin/sh to delete the log files after 3 days.

Hi,

you could do it with the find command:

find -mtime +5 |xargs rm -f

The above line will remove all files in that are older than 5 days.

For testing you could do:

find -mtime +5 | xargs ls -ld

Regards
Andreas Voss
Honored Contributor

Re: Unix shell script for bash shell #!bin/sh to delete the log files after 3 days.

Hi,

just an addition to make it safer:

find -name '*.log' -mtime +5 | xargs rm -f

This will look only for files which ends with .log

Regards
Mark Vollmers
Esteemed Contributor

Re: Unix shell script for bash shell #!bin/sh to delete the log files after 3 days.

If you wanted, you could take Andreas' line and set it up to run through a cron job to run daily or whatever. This would run command automatically, so you wouldn't have to type it in. To do this, you modify the cron table (type crontab -e to modify the table). The format for cron is

minute hour day month "day of week" command

For this, you could set it up daily to run at 5:00 pm (17:00), for example, every day of the week. This would be

0 17 * * * find -mtime +5 |xargs rm -f

The * denotes all, so this line would mean every day of the month, every month, every day of the week. You can set this up to run whatever fits your needs. Hope this helps!

Mark
"We apologize for the inconvience" -God's last message to all creation, from Douglas Adams "So Long and Thanks for all the Fish"
Dieter Degrendele_1
Frequent Advisor

Re: Unix shell script for bash shell #!bin/sh to delete the log files after 3 days.

Hi,

Just as the king said:

Go to the directory containing the log files (or the directory underneeth).

find ./ -name "*.log" -mtime +3 -exec rm{} \;

You have a few parameters with the find command of which these are frequently used:

-atime : access time (days)
-mtime : modified time (days)
-name : find certain filename
-exec : executes a command for every file found

For more info type man find.

Regards,
DD
The possible we did, the unpossible we're doing but for a miracle you have to wait some time.
Sandy_2
New Member

Re: Unix shell script for bash shell #!bin/sh to delete the log files after 3 days.

I tried to use this command on bash shell

find -name '*.log' -mtime +5 | xargs rm -f

like this by going to the particular directory where log file exist but it didn't work

find *.log -mtime +5 | xargs rm -f

Please help me
James R. Ferguson
Acclaimed Contributor

Re: Unix shell script for bash shell #!bin/sh to delete the log files after 3 days.

Hi Sandy:

When using the '-atime' '-mtime' or '-ctime' options of 'find', a "day" is a 24-hour period. For example, 'find' will fail to deliver a file that has been modified less than 24-hours ago if the '-mtime' option is used with '+1'.

This kind of precision is usually meaningless when you are merely pruning files after n-days. You'll remove the file in today's run or tomorrow's.

Perhaps this explains your observation.

...JRF...
Brian Markus
Valued Contributor

Re: Unix shell script for bash shell #!bin/sh to delete the log files after 3 days.

Hi Sandy,

I use find as well, but i use the following commands.

find /tmp/print -name '*.log' -type f -mtime +5 -exec rm {} \;

Hope this helps.
When a sys-admin say's maybe, they don't mean 'yes'!
Kevin Wright
Honored Contributor

Re: Unix shell script for bash shell #!bin/sh to delete the log files after 3 days.

Sandy,
The above lines work..
I use
find /var/tmp -mtime +30 -exec rm {} \;

However, bash shell is not #!/bin/sh..that is posix shell, bash shell #!/bin/bash..at least default is that on a linux box.