System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

Keep last 30 days of files in a directory

 
Fenglin
Regular Advisor

Keep last 30 days of files in a directory

Hi

What's the find command that keeps the last 30 days of data in a specific directory?

Regards
Feng Lin
10 REPLIES
James R. Ferguson
Acclaimed Contributor

Re: Keep last 30 days of files in a directory

Hi Feng:

# find /path -xdev -type f -mtime +30 -exec echo {} +

If you are satisfied with the results when you run this, change the 'echo' to 'rm'.

Regards!

...JRF...
Fenglin
Regular Advisor

Re: Keep last 30 days of files in a directory

Hi JRF

Your command works ok on HP-UX. Can you make it work on linux as well?

Regards
Feng Lin
likid0
Honored Contributor

Re: Keep last 30 days of files in a directory

It works ok in linux:

find /home -xdev -type f -mtime +30 -exec echo {} \;
Windows?, no thanks
likid0
Honored Contributor

Re: Keep last 30 days of files in a directory

also if you have a zsh shell on your systems:

# deletes all regular file in /Dir that are older than 3 hours

first try with ls:
$ ls /Dir/**/*(.mh+3)
And if it's ok with you, you cam remove it:
$ rm -f /Dir/**/*(.mh+3)
Windows?, no thanks
Hakki Aydin Ucar
Honored Contributor

Re: Keep last 30 days of files in a directory

>JRF : # find /path -xdev -type f -mtime +30 -exec echo {} +

it is good for 11i v2 ;
# find /home -xdev -type f -mtime +590 -exec echo {} +
/home/informix/.dtprofile /home/informix/update_statistics.sh /home/informix/.profile.orig . . .

but (correct me if I am mistaken but) actually does not seem to be working in 11i v1;

# find /tmp -xdev -type f -mtime +30 -exec echo {} +
#
Hakki Aydin Ucar
Honored Contributor

Re: Keep last 30 days of files in a directory

Addition;

of course it is working with 11i v1;

find /home -xdev -type f -mtime +30 -exec echo {} \;
Nafez ALNajjar
Frequent Advisor

Re: Keep last 30 days of files in a directory

Hi,
Try;

find / -ctime +31 -exec rm {} \;

Good Luck
Nafez ALNajjar
James R. Ferguson
Acclaimed Contributor

Re: Keep last 30 days of files in a directory

Hi (again) Feng:

> Hi JRF. Your command works ok on HP-UX. Can you make it work on linux as well?

What's not working for you?

Change '/path' to whatever directory you want to visit.

The use the '+' character to terminate the '-exec' argument is very efficient. Multiple arguments are collected and the '-exec' command run for them. If you chose the '\;' terminator you must escape (backslash) the ';' so that the shell doesn't see it. This form is much less efficient since the '-exec' command argument is spawned for _every_ argument it processes.

Regards!

...JRF...
Fenglin
Regular Advisor

Re: Keep last 30 days of files in a directory

Hi All

The one ending with the \ works for linux.

Thanks to all.

Regards
Feng Lin

Re: Keep last 30 days of files in a directory

>The one ending with the \; works for linux.

Yes. If you want performance, you'll need to use xargs there. Or port a real (Posix) shell there.