Perl script for finding directories with mtime

Need assistance in the perl script . Below script gives me the results of all the files and directories with mtime with no issues . But i wanted to have a file and specify all the directory locations and use that file as reference and get results . Any ideas are highly Appreciated .

#!/usr/bin/perl -w

use warnings;
use strict;
use Time::Local;
use File::Copy;

my @dirs = ("<path1>","<path2>","<path3>");
my %seen;
while (my $pwd = shift @dirs) {
    opendir(DIR,"$pwd") or die "Cannot open $pwd\n";
    my @files = readdir(DIR);
    closedir(DIR);
    foreach my $file (@files) {
        if (-d $file and ($file !~ /^\.\.$ARGV[0]$/) and !$seen{$file}) {
            $seen{$file} = 1;
            push @dirs, "$pwd/$file";
        }
        next if ($file !~ /\.$ARGV[0]$/i);
        my $mtime = (stat("$pwd/$file"))[9];
        print   "$pwd \t $file \t", scalar(localtime($mtime));
        print "\n";
    }
}

here what i tried as an example and implemented the same on the main script . But it works with only one path not multiple paths

#!/usr/bin/perl

use strict;
use warnings;
open (my $fh, '<', 'test.txt') || die "Could not open test.txt: $!\n";
my @test = grep { $_ !~ /^#/ } <$fh>;
print @test;

You may need to cut down the trailing newline.

#!/usr/bin/perl

use strict;
use warnings;
my @test;
open (my $fh, '<', 'test.txt') || die "Could not open test.txt: $!\n";
while(<$fh>)
{
  chomp;
  push(@test,$_) if($_ !~ /^#/);
}
print @test;

rajamadhavan

it doesnt work on the main script but your script works . How can i modify with yours in the main script .

@Ajay,
What issue do you see when you do

#!/usr/bin/perl -w

use warnings;
use strict;
use Time::Local;
use File::Copy;

my @dirs = ();
open (my $fh, '<', 'test.txt') || die "Could not open test.txt: $!\n";
while(<$fh>)
{
  chomp;
  push(@dirs,$_) if($_ !~ /^#/);
}

my %seen;
while (my $pwd = shift @dirs) {

### REST OF THE CODE ###

Thank you rajamadhavan . It works great now what i really missed was

()

in

my @dirs = ();

.Appreciate your help

---------- Post updated at 09:52 AM ---------- Previous update was at 09:50 AM ----------

Madhan once we run the script we get results . any inputs on how i can sort based on the data in perl . i know how to do in unix using

sort

but never done in perl

It's similar in Perl.

sort @array

sorts the array @array.
The documentation page has a lot more information on this: sort - perldoc.perl.org

1 Like

durden_tyler: Using the above code, can you give me some inputs on sorting based on the colum.