Equivalent of "ls -t" ?

S

Stan Brown

I need to get a list of all the files in a given directory, sorted in
oldest -> newest into an array for processing.

What's a good way to do this?
 
B

Brian McCauley

Stan Brown said:
I need to get a list of all the files in a given directory,

Use readdir (and its related functions).

See FAQ: How do I sort an array by (anything)?
oldest -> newest into an array for processing.

The function to get the age of a file is -M
What's a good way to do this?

To sort something (in this case filenames) according to some extrinsic
or computed property (in this case the age of the file with that name)
you can use the Schwartzian Transform (as per the FAQ).

<fish>

opendir my $dh, $dir or die $!;

my @files = map { $_->[0] }
sort { $b->[1] cmp $a->[1] }
map { [ $_, -M "$dir/$_" ] }
readdir $dh;

</fish>

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
A

Anno Siegel

Stan Brown said:
I need to get a list of all the files in a given directory, sorted in
oldest -> newest into an array for processing.

What's a good way to do this?

Here is one:

my $dir = '.';
opendir my $d, $dir or die "bah $dir: $!";
my @l = sort { -M $a <=> -M $b } readdir( $d);
print join "\n", @l, '';

This is fine for small directories. For lots of files you may consider
a Schwartzian transform to reduce the overhead of "-M" calls. (See
"perldoc -q sort" for that.)

Anno
 
A

Anno Siegel

Abigail said:
Stan Brown ([email protected]) wrote on MMMDCCXII September MCMXCIII in
<URL:\\ I need to get a list of all the files in a given directory, sorted in
\\ oldest -> newest into an array for processing.


ls -t orders them from newest to oldest. Anyway, here's a solution
(oldest to newest).

my $dir = ".";
opendir my $dh => $dir or die;
my @files = map {substr $_ => 8}
sort
map {sprintf "%08x$_" => (stat) [9], $_} grep {! /^\./} readdir $dh;
^^
That second "$_" seems to be a remnant of an earlier version. Otherwise,
it's a very true replication of "ls -tr", skipping files beginning with
"." and, cleverly, sorting alphabetically files that have the same mod-time.

Why the hex format?

Anno
 
S

Stan Brown

In said:
Stan Brown ([email protected]) wrote on MMMDCCXII September MCMXCIII in
<URL:\\ I need to get a list of all the files in a given directory, sorted in
\\ oldest -> newest into an array for processing.

ls -t orders them from newest to oldest. Anyway, here's a solution
(oldest to newest).
my $dir = ".";
opendir my $dh => $dir or die;
my @files = map {substr $_ => 8}
sort
map {sprintf "%08x$_" => (stat) [9], $_} grep {! /^\./} readdir $dh;
closedir $dh;
thanks for the help!

This works great in a small test program, but when I put this fragment in
to my bigger program (which is using "strict:). I get the following run
time error:

Can't use string ("/usr/images/20031026") as a SCALAR ref while "strict
refs" in use at ./make_movie.pl line 474.

"/usr/images/20031026" is the directory I'm trying to get a list of files
from.

How can I fix this?
 
A

Anno Siegel

Abigail said:
Stan Brown ([email protected]) wrote on MMMDCCXII September MCMXCIII in
<URL:\\ I need to get a list of all the files in a given directory, sorted in
\\ oldest -> newest into an array for processing.


ls -t orders them from newest to oldest. Anyway, here's a solution
(oldest to newest).

my $dir = ".";
opendir my $dh => $dir or die;
my @files = map {substr $_ => 8}
sort
map {sprintf "%08x$_" => (stat) [9], $_} grep {! /^\./} readdir $dh;
^^
That second "$_" seems to be a remnant of an earlier version. Otherwise,
it's a very true replication of "ls -tr", skipping files beginning with
"." and, cleverly, sorting alphabetically files that have the same mod-time.

Oh, but it fails for a file named "hundred%safe". Maybe the earlier version
was preferable :)

Why the hex format?

Anno
 
S

Stan Brown

In said:
Stan Brown ([email protected]) wrote on MMMDCCXII September MCMXCIII in
<URL:\\ I need to get a list of all the files in a given directory, sorted in
\\ oldest -> newest into an array for processing.
ls -t orders them from newest to oldest. Anyway, here's a solution
(oldest to newest).
my $dir = ".";
opendir my $dh => $dir or die;
my @files = map {substr $_ => 8}
sort
map {sprintf "%08x$_" => (stat) [9], $_} grep {! /^\./} readdir $dh;
closedir $dh;
thanks for the help!
This works great in a small test program, but when I put this fragment in
to my bigger program (which is using "strict:). I get the following run
time error:
Can't use string ("/usr/images/20031026") as a SCALAR ref while "strict
refs" in use at ./make_movie.pl line 474.
"/usr/images/20031026" is the directory I'm trying to get a list of files
from.
How can I fix this?

It would help if I posted the line I bet :-(

opendir my $dh => $$::src_dir or die;
 
S

Stan Brown

In said:
ls -t orders them from newest to oldest. Anyway, here's a solution
(oldest to newest).
my $dir = ".";
opendir my $dh => $dir or die;
my @files = map {substr $_ => 8}
sort
map {sprintf "%08x$_" => (stat) [9], $_} grep {! /^\./} readdir $dh;
closedir $dh;
thanks for the help!
This works great in a small test program, but when I put this fragment in
to my bigger program (which is using "strict:). I get the following run
time error:
Can't use string ("/usr/images/20031026") as a SCALAR ref while "strict
refs" in use at ./make_movie.pl line 474.
"/usr/images/20031026" is the directory I'm trying to get a list of files
from.
How can I fix this?
It would help if I posted the line I bet :-(
opendir my $dh => $$::src_dir or die;

Never mind!

Is there a rule that says you won't see your stupid typo, till you post it
for the world to see :-(
 
A

Anno Siegel

Abigail said:
Anno Siegel ([email protected]) wrote on MMMDCCXII
September MCMXCIII in <URL:news:[email protected]>:
[] > sort
[] > map {sprintf "%08x$_" => (stat) [9], $_} grep {! /^\./}
readdir $dh;
[] ^^
[] That second "$_" seems to be a remnant of an earlier version. Otherwise,
[...]

There was no earlier version. The format was intended to be '%08x%s';
it's just that the directories I tested on didn't have files with %'s
in their names, so the typo remained unnoticed.

Yes. I noticed that the first $_ being in error was a more likely
scenario... after I posted.
[] Why the hex format?

Because I know timestamps are 32 bit integers, which are at most 8
hex digits. I contemplated figuring out how many digits I needed,
but then I realized that being lazy would save memory, so I used
hex digits instead of normal digits.

Right.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Equivalent of ls -d 4
Writing "ls" under windows 39
ls in ftp fails 5
Modifying ls 7
Can anyone code this for me ? 1
system("ls") 17
equivalent 23
Hi Im a Womble 4

Members online

Forum statistics

Threads
474,266
Messages
2,571,075
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top