Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

rm old directory

by Anonymous Monk
on Jul 22, 2008 at 14:45 UTC ( [id://699324]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi , how do I remove 60 day old directory and its files ? Thank you,

Replies are listed 'Best First'.
Re: rm old directory
by marto (Cardinal) on Jul 22, 2008 at 14:55 UTC
Re: rm old directory
by swampyankee (Parson) on Jul 22, 2008 at 15:05 UTC

    First, define what you mean by "60-day old directory." Do you mean the directory was created 60 days ago or the oldest file contained within it is 60 days old?

    I believe that most O/S mark a directory as modified (change it's mod time) only if a file has been added or deleted (this includes moved), but not if a file has been changed, i.e., touching an existing file won't necessarily change the mod time on the including directory1.

    The actual function you would is either rmdir or rmtree from the File::Path module. Unlink won't remove directories, unless "you are superuser and the -U flag is supplied to Perl." It also warns against using unlink to remove directories in all circumstances.


    added in update

    1 …of course, these same comments apply to directories within the top level directory, and their sub-directories, and so forth. Recursion!


    Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

Re: rm old directory
by moritz (Cardinal) on Jul 22, 2008 at 14:57 UTC
Re: rm old directory
by shoness (Friar) on Jul 22, 2008 at 20:30 UTC
    unix%> /usr/bin/find . -type d -mtime +60 -exec /bin/rm -rf {} \;
    • replace "." with the directory you want to search below
    • "-type d" says "directories"
    • "-mtime +60" says "not modified in 60 days" (also consider "-atime" for "last accessed (aka "read") time" or "-ctime" for "change time", depending on how you measure "age".)
    • "-exec" passes matching directory names to "/bin/rm -rf".

    Edited: per moritz's suggestion, recommend "mtime" instead of "atime" although it's all up to you...

      Relying on atime is generally a bad idea. I for one disable atime tracking during mounting, because IMHO it's a bad idea to have a side effect from every read.

      On my box your script would thus remove many directories that should stay.

      So rather rely on mtime.

        This is probably an edge case, but we had a tftp directory that we were cleaning up based on last modified time. The problem we ran into is that Windows/Samba and Konqueror all maintained the original timestamps on the file. So when someone copied a file created back in 2006 over to the system it got deleted fairly quickly!

        Our workaround was to make sure the files were not being backed up and then use the last access time instead of the last modified time.

Re: rm old directory
by Anonymous Monk on Jul 22, 2008 at 22:20 UTC
    Well, in the bash shell I'd say something like:

    find . -mtime -59 -type d -print | xargs rm -rf
    (the argument may not be exactly right, do a man on find for the right args, and I'd also do whatever option xargs has to *not* execute if nothing comes down the pipe, but then this is a perl question, so ...)

    Well, so in perl I'd do something dumb like run the 'find' command above (without the rm), fiddle with the output in any way I felt reasonable, then either use a perl-based 'rm -rf' or run another shell command.

    Not very perl-ish, I'm afraid, and probably pushing the 'more than one way to do it' a bit too far, but if you wanted to perl-ize it more do a cpan lookup for a 'find' replacement (which I'm sure you could write or find - I mean, its just 'open directory', get its age, and recurse), and for a 'rm -rf' replacment).

    Oh, goodness: there is a 'find' at http://search.cpan.org/perldoc?File::Find but it doesn't check the status. Perhaps you should check out 'find2perl', to which you give my above script (or something similar), and it will spit out the perl... see http://search.cpan.org/~nwclark/perl-5.8.8/x2p/find2perl.PL

Re: rm old directory
by girarde (Hermit) on Jul 22, 2008 at 22:41 UTC
    This question is kind of OS specific.
Re: rm old directory
by DrHyde (Prior) on Jul 24, 2008 at 09:48 UTC
    I just threw last year's Yellow Pages in the bin. Maybe that'll work for you too.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://699324]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-25 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found