0

I was setting up a cron job where I wanted to delete log files older than 1 day. The command to do this is as below. I am doing this on a AWS Linux EC2 instance.

find /var/log/tomcat8/ -mindepth 1 -mtime +1 -delete

But what I want to achieve is I want to exclude .log files from getting deleted and want to just delete the files with .gz extension. Can any body let me know how I achieve that exclusion in find command.

1 Answer 1

1

Add a -name flag to your find command like:

find /var/log/tomcat8/ -mindepth 1 -mtime +1 -name "*.gz" -delete

This should isolate it to finding only older .gz files. To add additional options (like another filename type to look for), use the -o switch which acts like an 'OR' statement on your options. So, something like:

find /var/log/tomcat8/ -mindepth 1 -mtime +1 -name "*.gz" -o -name "*.log" -delete
3
  • How will I achieve multiple extension here? for example if I want to delete .gz and .out too? Sep 8, 2016 at 17:30
  • Also I get this warning "find: warning: you have specified the -mindepth option after a non-option argument -name, but options are not positional (-mindepth affects tests specified before it as well as those specified after it). Please specify options before other arguments." Sep 8, 2016 at 17:36
  • Hrm, I haven't run into that error before. What OS are you running this on? Also, you might try removing the -mindepth +1 argument, as I don't think it's really necessary, here.
    – Thomas N
    Sep 8, 2016 at 21:02

You must log in to answer this question.

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