Skip to content
Advertisement

Delete logs that ended with date format

I created the following cli in order to delete the logs with date format that oldest then 500 min

date format is:

 data-node.log.xxxx-xx-xx-[1-10]

the cli that should removed the logs

find /var/log/test/  -type f -mmin +500 -regextype sed -regex '.*.log.[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2,10}$' -delete

as we can see the logs still exists

ls -l /var/log/test/



-rw-r--r-- 1 root root 0 10:02 data-node.log.2019-12-14
-rw-r--r-- 1 root root 0 10:02 data-node.log.2019-12-15
-rw-r--r-- 1 root root 0 10:02 data-node.log.2019-06-16
-rw-r--r-- 1 root root 0 10:02 data-node.log.2020-01-17
-rw-r--r-- 1 root root 0 10:05 data-node.log.2020-01-1723
-rw-r--r-- 1 root root 0 10:05 data-node.log.2020-01-172334
-rw-r--r-- 1 root root 0 10:05 data-node.log.2020-01-17233434
-rw-r--r-- 1 root root 0 10:05 data-node.log.2020-01-1723343434

where I am wrong?

Advertisement

Answer

Your regex does not match the files. Change

'.*.log.[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2,10}$'

for

'.*.log.[0-9]{4}-[0-9]{2}-[0-9]{2,10}$'

since there’s no third hyphen (nor fourth date field).

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement