Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

del files that is 7 days or older

by Anonymous Monk
on Feb 15, 2001 at 00:11 UTC ( #58432=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm try to write a script that deletes files which are 7 days old (or older)
#!/usr/bin/perl chdir('/home/archive/logs/old'); @FileNum = system("ls -t -al") ; chomp($month = 'date +"%b" --date "7 day ago"'); chomp($date = 'date +"%d" --date "7 day ago"'); foreach (@FileNum) { if /how do I compar the date?? here { system(" rm $_"); } };
Can anyone help?

Comment on del files that is 7 days or older
Download Code
Re: del files that is 7 days or older
by dvergin (Monsignor) on Feb 15, 2001 at 00:21 UTC
    First, let's format things a little so we can see more clearly what you are sharing with us. If you put your code between <code>...</code> tags, it will keep its alignment. Here's your code again:
    #!/usr/bin/perl chdir('/home/archive/logs/old'); @FileNum = system("ls -t -al") ; chomp($month = 'date +"%b" --date "7 day ago"'); chomp($date = 'date +"%d" --date "7 day ago"'); foreach (@FileNum) { if /how do I compar the date?? here { system(" rm $_"); } };
Re: del files that is 7 days or older
by oakley (Scribe) on Feb 15, 2001 at 00:23 UTC
    Since it appears you are using a *nix system, why not try:
    find DIRECTORY_HERE -mtime +7 | xargs rm -f
    This would work great from either command line or a cronjob

    - oakley
    Embracing insanity - one twitch at a time >:)
Re: del files that is 7 days or older
by dvergin (Monsignor) on Feb 15, 2001 at 02:37 UTC
    You will want to familiarize yourself more with the system() function. It returns the exit status of the given command, not what goes to STDOUT. For that you would use back-ticks or qx//. Also your two chomp lines have normal single-quotes where you need back-ticks.

    Generally, I try to solve a problem in terms of the approach proposed by the questioner. But in this case, you have started down a rocky road that will involve the date::manip module or something similar in order to correctly handle months as alpha abbreviations (Jan, Feb, etc.) instead of numbers. It's do-able but more bother than it is worth probably. Here's a more perlish solution:

    #!/usr/bin/perl -w use strict; my $file; my $dir_spec = '/home/archive/logs/old'; opendir(LOGDIR, $dir_spec) or die "Can't open $dir_spec: $!\n"; while ( defined($file = readdir(LOGDIR)) ) { if (-M "$dir_spec/$file" > 7) { unlink("$dir_spec/$file") or die "Can't delete $dir_spec/$file: $!\n"; } } closedir(LOGDIR);
    The key to this is the file test operator -M which returns the number of days old the given file is. Just the thing you needed.
      I'm curious as to how the -M operator works. Since Unix doesn't keep track of file creation dates, does -M check for the last modified? So in the example above would the given file be deleted if it wasn't modified in the last 7 days? I'm also assuming that one could use the -M to test directories as well. Is this true? :]
        Take a look at stat. All the --something operators are just frontends for this function. UN*X keeps track of last access, last modification, and last inode change (often confused with creation time).

        -M looks at the mtime (modification time), and subtracts $^T (see perlvar). A better choice might be -C, which looks at ctime (inode change time). Still, that too just looks at the last time you changed something about the file "metadata": creation, permissions, and the like.

(dkubb) Re: (2) del files that is 7 days or older
by dkubb (Deacon) on Feb 15, 2001 at 06:14 UTC

    This will delete any files in the current directory that are 7 or more days old:

    perl -e 'unlink grep { -M >= 7 } <*>'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2014-09-03 08:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (35 votes), past polls