• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

Sorting a directory by last modified date

In UNIX, using Perl, what would be the code to get a directory listing (with no traversing subdirectories) of files with a particular extension, sorted by last date modified and stored in an array?

Thanks!
0
rmacmich
Asked:
rmacmich
1 Solution
 
ozoCommented:
@array = sort {-M $a <=> -M $b} <*.ext>
0
 
amitpagarwalCommented:

@array = `ls -1t *.extn`

- amit
0
 
thoellriCommented:
ozo,

that's supposed to be
@array = map { $_->[0] }
  sort { $a->[1] <=> $b->[1] }
  map { [$_, -M] }
  <*.ext>;

you posted the schwartzian transform to one of my comments a long time ago.

Tobias
0
 
ozoCommented:
#or using a generic Schwartzian Transform:
sub ST(&@){
        my $metric=shift;
        map {$_->[0]}
        sort {$a->[1] cmp $b->[1]}
        map {[$_,&{$metric}]} @_
}

@array = ST {pack'N',(stat)[9]} <*.bak>;
0
 
darinwCommented:
ozo, I have posted a new question for you in this topic area.

darinw
Customer Service
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now