5

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.

1

5 Answers 5

5

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 {} +
3
  • 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, 2010 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 Dec 15, 2010 at 14:10
  • could u pls tell any link to study shell script easily. Dec 16, 2010 at 5:39
10

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.

2

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.

0

Check this handy tool from debian tmpreaper

0

An alternative would be to use xargs, which is apparently more efficient — http://www.sunmanagers.org/pipermail/summaries/2005-March/006255.html

So you could do something like this:

find /export/home/ftp -maxdepth 1 \( -name console.log -or -name server.log \) -mtime +30 | xargs -O -r rm

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .