My 30-day log cleanup kept a file that was 30 and a half days old

●1 ●34 ●98
calendar_today • schedule1 min read

I set three log files to known ages with touch -d — exactly 30 days, 30 days and 12 hours, and 31 days — then ran what I thought was a 30-day retention policy:

find . -name '*.log' -mtime +30

It returned one file: the 31-day-old one. The 30.5-day file stayed. So did the 30-day file.

-mtime counts whole 24-hour periods and throws away the remainder, and +30 means strictly greater than 30 of them. A file aged 30 days and 12 hours counts as 30, and 30 is not greater than 30. A real 30-day policy is -mtime +29. Re-running with +29 returned all three, which is what I wanted in the first place.

Two more things that bit during the same session. Piping to xargs rm split app v2.log on the space — rm reported cannot remove 'ws/app' and cannot remove 'v2.log', and the file survived. And -delete is an action that returns true, so putting it before your filter is catastrophic: find ord -delete -name '*.log' removed a file called keep.txt and then the directory itself. Preview with -print, keep -delete last.

The multi-directory loop, the .gz variant, and the cron lines are in the find -mtime log retention walkthrough.

Want more #devops & #devops deep dives?

Join 5,973+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

2 Comments

1 vote
1

More Posts

The log was deleted. Its 48M was still allocated.

BashSnippets - Oct 28

I deleted 44.6 GB and df moved by 5

BashSnippets - Nov 10

I killed the process I started. The port stayed taken.

BashSnippets - Nov 5

I escaped the dot correctly and sed still rewrote a file marked DO NOT CHANGE

BashSnippets - Oct 13

One file more and rm deleted none of them

BashSnippets - Nov 18
chevron_left
3.7k Points • 133 Badges
North America • bashsnippets.xyz
77Posts
36Comments
15Connections
Linux user who got tired of Googling the same bash commands every time I sat down at a terminal. Sta... Show more

Related Jobs

View all jobs →

Commenters (This Week)

1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!