Piyush Patil
Piyush Patil

Reputation: 14533

Deleting old files using cron job in Linux

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.

Upvotes: 0

Views: 2008

Answers (2)

Amit Kaneria
Amit Kaneria

Reputation: 5808

Print all files selected as below:

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

Above files can be deleted as below:

find /var/log/tomcat8/ -name '*.gz' -mindepth 1 -mtime +1 -exec rm{} \

Upvotes: 0

helloV
helloV

Reputation: 52393

Just look for *.gz files and delete them.

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

Before deleting, just list the files to make sure you are deleting the correct ones.

find /var/log/tomcat8/ -name '*.gz' -mindepth 1 -mtime +1 -print

Upvotes: 4

Related Questions