Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
opendir my $dir, $dir;

This line opens a handle (which is saved as $d) to the directory found at path $dir (so $dir could be defined as '/home/myuser/thedir').

my @f = sort { -M $b <=> -M $a } readdir $d;

This is the sort of line that shows what perl can do. There's a couple of things going on here, so we can break it down to see what's happening. This statement basically works backwards. First the readdir($d) generates a list of files and directories being read from the $d directory handle. If the directory at $dir contains foo1.dat, foo2.dat, subDir1, subDir2, foo3.dat, then you will get those 5 items listed through readdir(). This list of files is then passed to the sort routine, which is the part enclosed in curly braces. The -M $b <=> -M $a sorts the list of files by the last modified time of the file -- the end result from this sort will be the list of files read from the readdir(), which we are placing in the @f array. Because of the way we've sorted the list, the oldest files will be at the front of the array, with the newest files at the end.

unlink @f[0,1] if @f > 20;

As I said earlier, the @f array now contains the list of files, with the oldest files listed at the front of the array (so elements 0 and 1 would be your oldest 2 files). So the conditional statement on the size of @f checks to see if we have more than 20 files, and if so, executes unlink() on the first 2 entries to have them deleted.

I'd probably rewrite the code as follows to also ensure we are only dealing with files, just in case you have any directories in that main directory we do not want to include. Warning: my perl is quite rusty, there is a good chance the following code will not run, and if it does, it may not work as expected.

my $dir = '/path/to/the/files'; opendir(my $d, $dir) or die("opendir() failed: $!"); my @sort_files = sort { -M($b) <=> -M($a) } grep { -f($_) } readdir($d); unlink(map { "$dir/$_" } @f[0, 1]) if (@sort_files > 20);

In reply to Re: No. files in folder by saskaqueer
in thread No. files in folder by Win

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (4)
    As of 2021-04-15 06:07 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?