Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How to Know how much old my file is

by ravi45722 (Pilgrim)
on Sep 15, 2015 at 06:54 UTC ( #1142007=perlquestion: print w/replies, xml ) Need Help??

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

I want to know How old are my files which are present in the directory. If it is older than seven days I want to delete it from the directory. For that I tried this.

#!/usr/bin/perl -- use strict; use warnings; my $command = ; my @files = `ls` ; my $old=0; foreach my $file (@files) { chomp $file; print $command; $old = `expr $(date +%s) - $(date +%s -r $file)`; print "$file","::",$old,"\n"; }

But in these the "$" sign in the command mismatches. I used escape(\) before that. Even its not working. How to do this

Replies are listed 'Best First'.
Re: How to Know how much old my file is
by Corion (Pope) on Sep 15, 2015 at 07:16 UTC

      Ya it's simpler & lighter. Working very well

Re: How to Know how much old my file is
by Discipulus (Abbot) on Sep 15, 2015 at 07:01 UTC
    Hello,
    is better to use Perl functions when they are! in this case stat is your tool. Modification time is the 9th element of the array returened by that function. Pay some attention to returned time value because they are expressed in seconds from epoch.
    Additionally you must be aware of some modification needed, for example, to years (add 1900). see the docs for the localtime function.

    Be aware of do not using the module(update 16 Sep) to understand the differences between the builtin function and the module: infact the module override the stat function and export a object oriented interface: see Problem with file::stat and Re: File date.

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      is better to use Perl functions when they are! ...Be aware of do not using the module: see Problem with file::stat and Re: File date.

      stat is like readdir, its low level, not very memorable, and easy to forget to do your own error checking

      Path::Tiny makes life very conveniently memorable and brings in File::stat

      $ perl -le " print scalar gmtime( ( stat shift )[9] ) " Thu Jan 1 00:00:00 1970 $ perl -le " print scalar gmtime( ( stat shift )[9] ) " . Tue Sep 15 07:20:27 2015 $ perl -le " print scalar gmtime( ( stat shift )[9] ) " .. Fri May 29 06:13:23 2009 $ perl -le "use Path::Tiny; print scalar gmtime( path( shift )->stat-> +mtime ) " Path::Tiny paths require defined, positive-length parts at -e line 1. $ perl -le "use Path::Tiny; print scalar gmtime( path( shift )->stat-> +mtime ) " . Tue Sep 15 07:20:27 2015 $ perl -le "use Path::Tiny; print scalar gmtime( path( shift )->stat-> +mtime ) " .. Fri May 29 06:13:23 2009
        yes you are somehow right. I just not explored yet the possibility of using Path::Tiny even if i have eard very well about it. Anyway i do not dislike the use of low levels: they are the foundamentals, usefull to know and understand, imho.
        perl -e "print scalar localtime((stat $ARGV[0])[9])" . Mon Jul 27 09:30:25 2015

        L*
        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: How to Know how much old my file is
by Laurent_R (Canon) on Sep 15, 2015 at 07:34 UTC
    Just a quick one-liner:
    $ perl -we ' print join " ", grep { -M > 7} glob ("*.pl"); > ' 1095341.pl a.pl add2.pl add_2.pl amazon.pl analyse_fable.pl ... while.pl X_oper.pl
Re: How to Know how much old my file is
by Anonymous Monk on Sep 15, 2015 at 07:24 UTC
    #!/usr/bin/perl -- use strict; use warnings; use Path::Tiny qw/ path /; for my $file ( path('.')->children ){ my $days = -M $file; if( $days > 7 ){ print qq{rm "$file" :: $days\n}; } } __END__
Re: How to Know how much old my file is
by pme (Prior) on Sep 15, 2015 at 07:01 UTC
Re: How to Know how much old my file is
by Anonymous Monk on Sep 15, 2015 at 12:29 UTC
    On a Unix/Linux box the logrotate utility can easily do such things, without programming on your part.

      Oh it's very useful. I will try it surely. Thanks

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2020-11-26 02:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?