Shell script should not delete any files under* root dir*. My* path will be like /export/home/ftp/ ...

I did some research and figured out the way for finding and deleting the files older than 30 days from a specific path, using find and exec commands.

*find /export/home/ftp/ -type f -mtime +30 -exec rm -f {} \;

But according to the requirement I want to delete console.log and server.log only from that directory and exclude the remaining files.

Please help me out in this issue.

link|improve this question

71% accept rate
So then is your title inaccurate? – Ignacio Vazquez-Abrams Dec 15 '10 at 4:34
feedback

3 Answers

up vote 4 down vote accepted

Assuming you really need to be using find in order to recurse through subdirectories:

find /export/home/ftp \( -name console.log -or -name server.log \) -mtime +30 -exec rm -f {} +
link|improve this answer
You could make it a little more efficient by adding -maxdepth 1 so that it doesn't actually recurse since you know which two files you want to check exactly. – sinping Dec 15 '10 at 13:31
I always forget that find has a -delete option. So let's amend that outright to: find /export/home/ftp -maxdepth 1 \( -name console.log -or -name server.log \) -mtime +30 -delete – jgoldschrafe Dec 15 '10 at 14:10
could u pls tell any link to study shell script easily. – Jayakrishnan T Dec 16 '10 at 5:39
feedback

If you just need to remove the old server.log and console.log every month you can also use logrotate which is most likely already running under RHEL. A config snippet like this will work in /etc/logrotate.d/*.conf or wherever the config files are located on your system.

# rotate server.log and console.log every month
# delete, not compress, old file

/export/home/ftp/server.log /export/home/ftp/console.log {
    monthly
    rotate 0
}

A custom monthly cron, as suggested above, will also work well. In fact, since logrotate is run from cron, you could consider this a cron extension of sorts. HTH.

link|improve this answer
feedback

Why not just use a monthly cron?

@monthly /usr/bin/rm -f console.log @monthly /usr/bin/rm -f server.log

It would definitely be safer then doing things with find.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.