Operating System - HP-UX
1833875 Members
1612 Online
110063 Solutions
New Discussion

Re: Finding files that are n days before date

 
SOLVED
Go to solution
Khalid A. Al-Tayaran
Valued Contributor

Finding files that are n days before date


Hi,

How can I find files in a directory that are for example 3 or 4 days old using the find command:

find -atime "????" /data1 -print

Thanks,,,
8 REPLIES 8
Pete Randall
Outstanding Contributor
Solution

Re: Finding files that are n days before date

How about

find /data1 -mtime +3

?

Pete

Pete
Jean-Louis Phelix
Honored Contributor

Re: Finding files that are n days before date

hi,

As usual Pete is fast and rigth ... The complete syntax from the man page states :

In the descriptions of the primaries, the argument n represents a decimal integer; +n means more than n, -n means less than n, and n means exactly n.

so :

3 days old --> -mtime 3
more than 3 days old --> -mtime +3
less than 3 days old --> -mtime -3

Regards
It works for me (© Bill McNAMARA ...)
James R. Ferguson
Acclaimed Contributor

Re: Finding files that are n days before date

Hi:

# touch -mt 11160000 /tmp/ref
# cd
# find . ! -newer /tmp/ref

...finds files older than 11/16/2002...

# find . -newer /tmp/ref

...find files modified since 11/16...

Regards!

...JRF...


harry d brown jr
Honored Contributor

Re: Finding files that are n days before date

find / -type f \( -mtime +1 -a \( ! -mtime +2 \) \) -exec ls -l {} \;

live free or die
harry
Live Free or Die
Khalid A. Al-Tayaran
Valued Contributor

Re: Finding files that are n days before date



Hi,

Thanks gurus,,,

That was fast. Faster than news flash thses days...


Appriciate your help.
James R. Ferguson
Acclaimed Contributor

Re: Finding files that are n days before date

Hi (again):

The use of 'mtime' (and its cousins 'atime' and 'ctime') base "day" on a 24-hour interval. In most cases this yields the user what is sought. If more granularity in time is desired when isolating files, the use of '-newer' with a reference file is an appropriate choice.

Regards!

...JRF...
Khalid A. Al-Tayaran
Valued Contributor

Re: Finding files that are n days before date


Hi again,,

I have 2 directories data1 and data2. Say under each is:

.../data1/sales
.../data1/logs
.../data1/suppliers

.../data2/sales
.../data2/logs
.../data2/suppliers

I want to search both directories and delete (rm -r) the sales directory and all files below it. "" Safely "". How to do that ??And what if one of the directories names are randomly generated by some program ?? say data1/bcgfrty
(Remove the bcgfrty directory) ??



harry d brown jr
Honored Contributor

Re: Finding files that are n days before date


To remove the "sales" directories, just do

rm -rf .../data1/sales .../data2/sales

With of course replacing the "..." with the appropriate "PATH". Now if you have more than just "data1" and "data2", say like up to data9, then this will work:

rm -rf .../data[0-9]/sales


As for "RANDOMLY" created directory names, you could do this to delete those and the "sales" directories:

cd to appropriate directory

find ./data[0-9]/* -type d -depth ! -name suppliers -a ! -name logs -exec rm -rf {} \;

OF COURSE, YOU SHOULD NEVER JUST EXECUTE A rm -rf WITHOUT FIRST DETERMINING IF IT IS GOING TO DO WHAT YOU WANT IT TO DO, so do this first:

find ./data[0-9]/* -type d -depth ! -name suppliers -a ! -name logs -exec echo rm -rf {} \;

live free or die
harry
Live Free or Die