Skip to content

Instantly share code, notes, and snippets.

@csharpforevermore
Created October 27, 2013 21:09
Show Gist options
  • Save csharpforevermore/7187926 to your computer and use it in GitHub Desktop.
Save csharpforevermore/7187926 to your computer and use it in GitHub Desktop.
List the contents of a directory - by creation date. I use this script for when downloading large numbers of files and need to rename them later, although there are many other uses of course.
use 5.12.0;
use warnings;
use File::stat;
my $dir_name = shift;
if ( not defined $dir_name ) {
die qq(Usage: $0 <directory>);
}
opendir(my $dir_fh, $dir_name);
my @file_list;
while ( my $file = readdir $dir_fh) {
if ( $file !~ /^\./ ) {
push @file_list, "$dir_name/$file"
}
}
closedir $dir_fh;
say scalar @file_list;
for my $file (sort {
my $a_stat = stat($a);
my $b_stat = stat($b);
$a_stat->ctime <=> $b_stat->ctime;
} @file_list ) {
say "$file";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment