Operating System - HP-UX
1819734 Members
3033 Online
109606 Solutions
New Discussion юеВ

Re: Script to delete outdated files

 
SOLVED
Go to solution
Ed Hon
Regular Advisor

Script to delete outdated files

I want to write a script to examine the files in a directory (non-recursively) and delete the ones over x days old. How do I perform the date comparison?
21 REPLIES 21
Stefan Farrelly
Honored Contributor

Re: Script to delete outdated files

use the find command, it does the date comparison for you;

cd
find . -mtime +7 -exec rm {} \;

This will delete all files in this dir older than 7 days.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Pete Randall
Outstanding Contributor

Re: Script to delete outdated files

I was going to suggest they find command as well but I'm not sure how to make it non-recursive which I assume to mean that you don't want to traverse underlying subdirectories. I assume you'll have to write a script using Clays caljd.sh to do date comparisons.


Pete


Pete
Jeff Schussele
Honored Contributor

Re: Script to delete outdated files

Hi Ed,

To do the find nonrecursively add
! -depth
So Stefan's command would be

find . ! -depth -mtime +7 -exec rm -i {} \;

Note I added the -i to rm to have it query you just in case you find files you wish to retain.
If it's a long list & you don't want answer Y/N to them all then just replace rm with ll to get a listing & remove the -i

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Pete Randall
Outstanding Contributor

Re: Script to delete outdated files

(duh!! - thanks, Jeff!)

Pete
Ralph Grothe
Honored Contributor

Re: Script to delete outdated files

If you don't want to recurse, does it mean you'd simply want to stat files in the selected directory?
Sorry, for my silly question but English isn't my native tongue.

If so then I doubt that the suggested "! -depth" will work.
Instead you should use find's -prune switch.
But I guess fiddling together an appropiate -prune logic will be more cumbersome than putting together a few lines of Perl (or even C if you prefer) that simply use the syscalls opendir() and readdir().

If you don't want to engulf submounts in the find you'd have to use the -xdev switch.
Madness, thy name is system administration
Jeff Schussele
Honored Contributor

Re: Script to delete outdated files

I stand corrected - -prune it is.
Good catch Ralph.

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Ed Hon
Regular Advisor

Re: Script to delete outdated files

Jeff,

If cd to /dumpdat1 and do

find . -depth -mtime +3

I get as output several lines with file names in /dumpdat1, but if I do

find . ! -depth -mtime +3

all my output disappears.

Ed
Ed Hon
Regular Advisor

Re: Script to delete outdated files

find . -prune -mtime +3
find . ! -prune -mtime +3

Either gives me no output.

Ed
Leif Halvarsson_2
Honored Contributor

Re: Script to delete outdated files

Hi,
try

find . -mtime +7 -print |while read a
do
if [ $(dirname $a) = "." ]
then
rm $a
fi
done
Stefan Farrelly
Honored Contributor
Solution

Re: Script to delete outdated files

sorry, the format for find to delete files ONLY in the current dir is;

find . \( -type d ! -name . -prune \) -o \( -type f -mtime +7 ! -name "." -print \) -exec rm {} \;

Try it, works fine.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Todd McDaniel_1
Honored Contributor

Re: Script to delete outdated files

Ed,

Your syntax is wrong... mtime should come first. refer to the man page also for full syntax

YES:
find . -mtime +3 -prune

NO:
find . -prune -mtime +3
Unix, the other white meat.
Todd McDaniel_1
Honored Contributor

Re: Script to delete outdated files

Ed,

Your syntax is wrong... mtime should come first. refer to the man page also for full syntax

YES:
find . -mtime +3 -prune

NO:
find . -prune -mtime +3
Unix, the other white meat.
Todd McDaniel_1
Honored Contributor

Re: Script to delete outdated files

Ed,

Your syntax is wrong... mtime should come first. refer to the man page also for full syntax

YES:
find . -mtime +3 -prune

NO:
find . -prune -mtime +3
Unix, the other white meat.
Todd McDaniel_1
Honored Contributor

Re: Script to delete outdated files

Ed,

Your syntax is wrong... mtime should come first. refer to the man page also for full syntax

YES:
find . -mtime +3 -prune

NO:
find . -prune -mtime +3
Unix, the other white meat.
Todd McDaniel_1
Honored Contributor

Re: Script to delete outdated files

Ed,

Your syntax is wrong... mtime should come first. refer to the man page also for full syntax

YES:
find . -mtime +3 -prune

NO:
find . -prune -mtime +3
Unix, the other white meat.
Todd McDaniel_1
Honored Contributor

Re: Script to delete outdated files

Ed,

Your syntax is wrong... mtime should come first. refer to the man page also for full syntax

YES:
find . -mtime +3 -prune

NO:
find . -prune -mtime +3
Unix, the other white meat.
Todd McDaniel_1
Honored Contributor

Re: Script to delete outdated files

Ed,

Your syntax is wrong... mtime should come first. refer to the man page also for full syntax

YES:
find . -mtime +3 -prune

NO:
find . -prune -mtime +3
Unix, the other white meat.
Todd McDaniel_1
Honored Contributor

Re: Script to delete outdated files

Ed,

Your syntax is wrong... mtime should come first. refer to the man page also for full syntax

YES:
find . -mtime +3 -prune

NO:
find . -prune -mtime +3
Unix, the other white meat.
Ralph Grothe
Honored Contributor

Re: Script to delete outdated files

See Stefan, that's what I meant.
The usage of -prune switches soon becomes quite nasty, especially when you have to quote all those parantheses to protect from the shell.
That's why I'd prefer something Perlish like

e.g.


opendir DH, '/tmp' or die "opendir failed: $!\n";
@files = grep {! -d "/var/$_"} grep !/^[.]{1,2}$/, readdir DH;


Now if you're a hot shot you could do something like

unlink map("/tmp/$_", @files);
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: Script to delete outdated files

Sorry, I haven't considdered the filtering of proper mtimes.
But this should be easy to insert in the grep() call.
Madness, thy name is system administration
Ed Hon
Regular Advisor

Re: Script to delete outdated files

This forum is awesome! Thanks everyone. With one small mod (changed second -name from "." to ".*") to ignore system files, I chose Stefan's solution:

find . \( -type d ! -name . -prune \) -o \( -type f -mtime +7 ! -name ".*" -print \) -exec rm {} \;

Sorry, I don't know Perl, but I heard she's a blast.