Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Delete Old Directories

by Marshall (Abbot)
on Oct 20, 2011 at 02:19 UTC ( #932543=note: print w/replies, xml ) Need Help??

in reply to Delete Old Directories

I will venture forth with some advice:

1). Use gmt (UTC) - dealing with local time and DST vs regular time is a mess. Eliminate that by only using UTC. Convert to local time only for user presentation when necessary.

2). Use a directory name format that is sortable by normal alphanumeric sort: YYYYMMDD if month is 1, then use 01. If day is 9, use 09. A date in this format can be compared and sorted against other dates using just a simple cmp instruction. Leading zeroes matter.

3). I would only use the "easily visible stuff", ie, the directory name (probably an easily parse-able part of that name). Although you are going to be creating directories on the "right dates", etc. and the -M time, etc should be the same, go with the name of the file. We want to have things that happen in the most obvious way possible and the obvious ways for users to list the files work should work. Please pay attention to (1) and (2).

I hope these 3 points will save you a lot of grief!

So, each dir name should have an easy way to parse YYYYMMDD out of it. Calculate the oldest YYYYMMDD date as a string that you want to keep. Use string compares of the regex captured date string of the file/directory name to decide whether this file/directory should be kept or not.
delete (this file/directory) if ($thisdate lt $oldestdate);

Replies are listed 'Best First'.
Re^2: Delete Old Directories
by Anonymous Monk on Oct 20, 2011 at 10:51 UTC
    All that make sense, but if I have in this directory other directories like:
    09052011 10152011 10162011 10172011 10182011 10192011 10202011
    How could I compare these dates from the directory names if they are 45 days older than today's date. With the dates above I would have to delete 09052011 directory because its 45 days older than today's date.
      Your date format is different from the proposed one. To make things easy, you should be using this one:
      20110905 20111015 20111016 20111017 20111018 20111019 20111020
      Then, just generate the string for the day 45 days ago and remove everything lesser.
      How to find what that days' date was? How yould you proceed without a computer? Would you calculate the date, or just have a look into a calendar?
        This is exactly the right idea! The file names shown will sort with a simple string comparison based upon the text from the file name. I am at a loss as to how to make this more clear.

        Working with the filename is just much easier and more obvious than working with OS specific things like the -M file test.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://932543]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2018-06-24 19:43 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.