LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Find with -mtime is finding EVERYTHING since, not everything older than (https://www.linuxquestions.org/questions/linux-newbie-8/find-with-mtime-is-finding-everything-since-not-everything-older-than-4175612050/)

NobleOne 08-16-2017 07:12 AM

Find with -mtime is finding EVERYTHING since, not everything older than
 
I have a backup directory created each night with over 100 Gb of data in it. As you might expect, it is organized with sub-directories and files within each.

I simply want to delete the entire tree where the highest level sub-directory is over x days old.

First, the ls showing the contents:

ls -l
total 12
drwxr-xr-x. 34 root root 4096 Aug 14 15:25 20170814-102714
drwxr-xr-x. 34 root root 4096 Aug 15 03:43 20170815-020101
drwxr-xr-x. 34 root root 4096 Aug 16 05:42 20170816-020101

Here, the find showing what -mtime -2 returns:

find . -maxdepth 1 -type d -mtime -2 -print
.
./20170815-020101
./20170814-102714
./20170816-020101

I know how to use -exec and may actually pipe the output to further shell commands before the rm -r.

My question is why is find returning all 3 directories?

Turbocapitalist 08-16-2017 07:21 AM

You'll need to use +2 instead of -2. The former will be older than 2 days the latter younger than 2 days.

Maybe the -daystart option would also help, too. See "man find" and scroll down to -mtime and -daystart

NobleOne 08-16-2017 07:25 AM

Thanks, but using + returns nothing, even if I use +1. Same results with/without -daystart

Just as further clarification, these directories are created and never touched again. So, the creation date = modification date.
I have also tried -ctime but no better results.

NobleOne 08-16-2017 07:26 AM

find . -maxdepth 1 -type d -mtime +2 -print
[root@NPLinux backup]# find . -maxdepth 1 -type d -mtime +2 -daystart -print
[root@NPLinux backup]# find . -maxdepth 1 -type d -mtime +1 -print
[root@NPLinux backup]# find . -maxdepth 1 -type d -mtime +3 -print
[root@NPLinux backup]# pwd
/home/backup
[root@NPLinux backup]# find . -maxdepth 1 -type d -mtime +1 -daystart -print

Turbocapitalist 08-16-2017 07:38 AM

Quote:

Originally Posted by NobleOne (Post 5748851)
Thanks, but using + returns nothing, even if I use +1. Same results with/without -daystart

The -daystart option must be before -mtime to work, but that would not explain the missing results.

NobleOne 08-16-2017 07:46 AM

That did it! Although I would have expected +1 to return the backups from 8/14 and 8/15. We are well beyond 24 hours from the 8/15 backup.

find . -maxdepth 1 -type d -daystart -mtime +1 -print
./20170814-102714
[root@NPLinux backup]# find . -maxdepth 1 -type d -daystart -mtime +2 -print
[root@NPLinux backup]#

The first of these backups was not created via cron job, but manually in the afternoon of 8/14. In the future, they will all be at 3 AM. So, the fact that the "+2" test didn't return anything wasn't too surprising. I will try it with +2 later today before finalizing the purge script and implementing in cron.

Thanks so much!

Turbocapitalist 08-16-2017 07:50 AM

No problem. You can also use touch to manually set a specific time on specific directories if that is needed in the beginning.


All times are GMT -5. The time now is 12:24 PM.