jessica
jessica

Reputation: 2610

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?

Upvotes: 1

Views: 255

Answers (2)

WGRM
WGRM

Reputation: 129

You made two logical errors, while composing your regex:

1. '.*.log.[0-9]{4}-[0-9]{2}-[0-9]{2} - [0-9]{2,10}$ This minus will not exist.

2. '.*.log.[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9] {2,10} $ This quantor is wrong. The file names can, but will not necessarily contain 2 upto 10 extra numbers. Therefore the endline $ is wrong, too.

This will work:

find /var/log/test/ -type f -mmin +500 -regextype sed -regex '.*\.log\.[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}[0-9]*' -exec rm {} +

Exec + will stack the command with as many filenames possible.

Upvotes: 0

samthegolden
samthegolden

Reputation: 1490

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

Upvotes: 1

Related Questions