Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find ./ -mtime -7 -exec ls -l {} \;

Status
Not open for further replies.

ui05067

MIS
Aug 24, 2001
18
US
the above command pulls more than just the last 7 days of modified files.

I am getting files well beyond 7 days ago.
 
Change this:-
find ./ -mtime -7 -exec ls -l {} \;

To this:-
find ./ -mtime 7 -exec ls -l {} \;


 
7 will get EXACT 7x24hr mod times - not what he wants.
The "extra files" are there because "exec ls -l {} \;" pulls contents of directories as well. So, to make it right and save CPU cycles on each "exec ls" do this:
find ./ -mtime -7 -ls
Done.
Or, if you do not need directories that changed in the last 7 days AND want to keep your notation:
find ./ -type f -mtime -7 -exec ls -l {} \;
I would not do that tho - inefficient.
 
Code:
#!/usr/bin/perl

use strict;
use File::Find;

if ($ARGV[0] eq "") { $ARGV[0]="."; }

my @file_list;
find ( sub {
  my $file = $File::Find::name;
   if ( -f $file ) {
    push (@file_list, $file)
  }
}, @ARGV);

my $now = time();       
my $AGE = 60*60*24*7;  


for my $file (@file_list) {
  my @stats = stat($file);
  if ($now-$stats[9] < $AGE) { 
    print "$file\n";
  }
}

Invoked as
$./myCmd.pl /myfilesystem # if no argument then it will use cwd.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top