Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
So then is your title inaccurate? – Ignacio Vazquez-Abrams Dec 15 '10 at 4:34
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 {} +
share|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

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 {
    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.

share|improve this answer

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.

share|improve this answer

Check this handy tool from debian tmpreaper

share|improve this answer

An alternative would be to use xargs, which is apparently more efficient —

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
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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