0

This is my script:

find /backup/dir -mindepth 1 -depth -mtime +4 -exec rm -rf \{\} \;

And after this I have always not last 4 backups but 5.

I think find sucks for this. Maybe someone have any idea how to use other tool in this?

1 Answers1

1

I think find sucks for this. Maybe someone have any idea how to use other tool in this?

find may still be the right tool.

From man find:

-mtime n
File's data was last modified n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file modification times.

[…]

Numeric arguments can be specified as

+n for greater than n,
[…]

The relevant comment is

When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.

This means 0 is possible. With -mtime +4 backups that report 0, 1, 2, 3 or 4 are left intact. I assume your backups are daily so each of the mentioned numbers is reported exactly once. Five numbers, five backups don't pass the test, five backups remain.

If you use -mtime +3 instead of -mtime +4 then backups that report 0, 1, 2 or 3 will remain; probably four backups total, the number you wanted.

See also Why does the behaviour of find differ using -mtime -0 vs. +0?