LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-16-2017, 07:12 AM   #1
NobleOne
LQ Newbie
 
Registered: Jul 2017
Posts: 29

Rep: Reputation: Disabled
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?
 
Old 08-16-2017, 07:21 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,303
Blog Entries: 3

Rep: Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720
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
 
Old 08-16-2017, 07:25 AM   #3
NobleOne
LQ Newbie
 
Registered: Jul 2017
Posts: 29

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-16-2017, 07:26 AM   #4
NobleOne
LQ Newbie
 
Registered: Jul 2017
Posts: 29

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-16-2017, 07:38 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,303
Blog Entries: 3

Rep: Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720
Quote:
Originally Posted by NobleOne View Post
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.
 
1 members found this post helpful.
Old 08-16-2017, 07:46 AM   #6
NobleOne
LQ Newbie
 
Registered: Jul 2017
Posts: 29

Original Poster
Rep: Reputation: Disabled
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!
 
Old 08-16-2017, 07:50 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,303
Blog Entries: 3

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


Reply

Tags
find, mtime



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Files older than 1 day not deleted with –mtime +1 RHCE_ran Linux - Enterprise 6 07-17-2015 11:15 AM
Find command with -mtime +15 or -15 days?? Virtuose Linux - Newbie 1 02-02-2011 05:48 PM
[SOLVED] Command to find only mtime ddenton Linux - General 3 08-12-2010 02:57 PM
using find to compare file mtime with another file's mtime TheFueley Linux - Newbie 1 12-23-2008 08:06 PM
mtime in find vs ls lslade Linux - Software 0 11-17-2005 01:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration