To delete logs older than 30 days

I want to write a shell script that deletes all log files in a directory that are older than 30 days except for 3 files:

I am using the following command:

find /tmp/logs -name "*.log" -mtime +30 -exec rm -f {} \;

But this command deletes all the log files.

How can i modify this script that all logs are deleted except for following files:

1-SQLUpdate.log
2-updated_days.log
3-qadirectsvcd.log

execute the below command. and check whether it ignores the mentioned files.

 
find /tmp/logs -name "*.log" -mtime +30 -a ! -name "SQLUpdate*" -a ! -name "updated_days*" -a ! -name "qadirectsvcd*"

If you are satisfied with the output, then add the -exec part

$ find /tmp/logs ! \( -name SQLUpdate.log -o -name updated_days.log -o -name qadirectsvcd.log \) -mtime +30 -exec rm {} \;

jayan you missed the -name "*.log"

otherwise it will delete other extension files also :frowning:

Yup .. missed it due to sleepless nights :slight_smile: Thanks ..

Thanks to all of you for the help. Following is the final command:

find . -name "*.log" -mtime +30 -a ! -name "SQLUpdate.log" -a ! -name "updated_days*" -a ! -name "qadirectsvcd*" -exec rm -f {} \;