Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Hide my ageing work

by blowupp (Novice)
on Oct 23, 2008 at 19:25 UTC ( [id://719132]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks - not fit to carry your night soil out - I've been lurking my way round the site & decided to prey for help:

I would like to prefix a dot to all files that are older than xx days in a main directory and also those in its subdirectories.

I found this in the archives but have stalled

#! /usr/local/bin/perl
die unless chdir "/path/directory";
die unless opendir DIR, ".";
foreach $file (grep {-f && (14 < -M)} readdir DIR) { mv $file .$file;
} closedir DIR;

It would help me learn if someone could explain what {-f && (14 < -M)} does.
thanks
Mark

Replies are listed 'Best First'.
Re: Hide my ageing work
by gone2015 (Deacon) on Oct 23, 2008 at 19:39 UTC

    -f "File is a plain file"

    -M "Age of file (at startup) in days since modification

    Testing $_ by default.

    But what I really wanted to know was: how long have you been suffering from feelings of being hunted ?

      "How long have you been suffering from feelings of being hunted"

      Oh, I see what you did there. I caught the misspelling as well ;D

      I'm so adjective, I verb nouns!

      chomp; # nom nom nom

Re: Hide my ageing work
by jettero (Monsignor) on Oct 23, 2008 at 19:33 UTC
    It's actually the code-block fed to grep using file test operators (-f and -M). I'm a little surprised they work on $_ (implicitly) and actually think the second one should use _ (also discussed on the file test op page) instead of $_.

    -Paul

Re: Hide my ageing work
by ccn (Vicar) on Oct 23, 2008 at 19:42 UTC
    {-f && (14 < -M)} is equivalent of
    {14 < -M $_ if -f $_;}
    see: perldoc -X
Re: Hide my ageing work
by Your Mother (Archbishop) on Oct 23, 2008 at 20:58 UTC

    You got some good answers already but you might try this-

    use Acme::Botox;

    But I keed!

    I'm also with jettero in surprise that the code works right. readdir doesn't return paths, just names. From the pod-

    If you're planning to filetest the return values out of a "readdir", you'd better prepend the directory in question. Otherwise, because we didn't "chdir" there, it would have been testing the wrong file.

    opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!"; @dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR); closedir DIR;
      Thanks for that,
      My script does not work and is only something I copied from the archives,
      Probably should have made it clearer that I dont know what I'm doing and joined here for some pointers .... I'm still laughing at ccn's reply; surely most humorous :-)
      Anyway, I will take the kind answers from all and try harder.
      cheers
      Mark
Re: Hide my ageing work
by eighty-one (Curate) on Oct 23, 2008 at 19:40 UTC
Re: Hide my ageing work
by periapt (Hermit) on Oct 24, 2008 at 13:49 UTC

    oshalla and eight-one gave good descriptions of the grep block

    The code almost works. Here are a couple of mods to make things work

  • The variable $file requires a my declaration
  • 'mv' is not a native perl function, use rename file1, file2. You could also use system() but, on my computer, that function seemed to work a little slower
  • You need to put double quotes around the second file otherwise perl interprets the '.' as the concatenation operator
  • Here is the modified code

    #! /usr/local/bin/perl use strict; use warnings; use diagnostics; die unless chdir "/path/directory"; die unless opendir DIR, '.'; foreach my $file (grep {-f && (14 < -M)} readdir DIR) { # print $file,"\n"; rename $file, ".$file"; # system("mv", $file, ".$file"); } closedir DIR;

    PJ
    use strict; use warnings; use diagnostics;
      With all the suggested weekend reading something has rubbed off & I have disturbed the Monks again to boast that I've cracked it!
      Here is my perl effort; A script to rename aged files, for the Monks archives;


      #! /usr/local/bin/perl
      use strict;
      use warnings;
      use File::Find;
      #set the age tolerance
      my $limit = 16;

      #call the find subroutine
      find (\&CheckFile, "/path to your directory");
      #subroutine
      sub CheckFile {
      $File::Find::name;
      my $age = -M;
      #test the age
      if (-f && ($age > $limit)) {
      print $File::Find::name;
      print " is ageing at ",int($age)," days old\n";
      rename ( $_, "XYZ.$_;") or die "rename failed: $!";
      }
      else {
      print $File::Find::name;
      print " is a youthful ",int($age)," days old\n";
      }
      }


Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-24 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found