Modifying ls

M

manian.k

I want to some how modify the contents of a ls listing to only display
the filename and the time it was modified.

Example if I do ls -lt
drwx------+ 1 mkrishnamoorthy Domain Users 0 Feb 16 17:32
Application Data
-rwx------+ 1 mkrishnamoorthy Domain Users 188757 Feb 8 16:12
trial.pdf
-rwx------+ 1 mkrishnamoorthy Domain Users 349544 Feb 8 15:55
IEEEtran.tar.

I just want the time and the file name, that is

Feb 16 17:32 Application Data

How can I push that information into an array.
$array = 'ls -t'
and usr array[1] to get the latest file.

Thanks
 
J

John W. Krahn

I want to some how modify the contents of a ls listing to only display
the filename and the time it was modified.

Example if I do ls -lt
drwx------+ 1 mkrishnamoorthy Domain Users 0 Feb 16 17:32
Application Data
-rwx------+ 1 mkrishnamoorthy Domain Users 188757 Feb 8 16:12
trial.pdf
-rwx------+ 1 mkrishnamoorthy Domain Users 349544 Feb 8 15:55
IEEEtran.tar.

I just want the time and the file name,

Then don't use ls.
that is

Feb 16 17:32 Application Data

How can I push that information into an array.
$array = 'ls -t'

$array is a scalar not an array.
and usr array[1] to get the latest file.

If you did that then the latest file would be in $array[0] not $array[1].


Here's how to do it without calling an external program like ls:


opendir DIR, '.' or die "Cannot open '.' directory: $!";

my $latest = [ '', 0 ];

while ( my $file = readdir DIR ) {

my $mtime = ( stat $file )[ 9 ];

$latest = [ $file, $mtime ] if $latest->[ 1 ] < $mtime;
}

closedir DIR;

print "The latest file is $latest->[0].\n";




John
 
T

Tad McClellan

Example if I do ls -lt
drwx------+ 1 mkrishnamoorthy Domain Users 0 Feb 16 17:32
Application Data

I just want the time and the file name, that is

Feb 16 17:32 Application Data

How can I push that information into an array.


my @array = map { chomp; substr($_, 43) } qx/ ls -lt /;
 
C

Charles DeRykus

I want to some how modify the contents of a ls listing to only display
the filename and the time it was modified.

Example if I do ls -lt
drwx------+ 1 mkrishnamoorthy Domain Users 0 Feb 16 17:32
Application Data
-rwx------+ 1 mkrishnamoorthy Domain Users 188757 Feb 8 16:12
trial.pdf
-rwx------+ 1 mkrishnamoorthy Domain Users 349544 Feb 8 15:55
IEEEtran.tar.

I just want the time and the file name, that is
jjjjjjhh
Feb 16 17:32 Application Data

How can I push that information into an array.
$array = 'ls -t'
and usr array[1] to get the latest file.

Here's a Unix-centric solution to get the latest file and its timestamp
but see the opendir solution for more portability:

my @ls=`ls -lt`;
die "ls command failed: $?" if $?;
@ls=split ' ', $ls[1];
@array[0,1] = ( "@ls[5..7]", "@ls[8..$#ls]" );
 
B

Bart Lateur

How can I push that information into an array.
$array = 'ls -t'

Use
@array = `ls -t`;

instead. $array is not an array. `` will call the program and capture
the output.
and usr array[1] to get the latest file.

make that

$array[0]

Which will pick the first array item. Warning: it'll have a newline
appended.
 
A

Anno Siegel

John said:
(e-mail address removed) wrote:
[...]
How can I push that information into an array.
$array = 'ls -t'

$array is a scalar not an array.
and usr array[1] to get the latest file.

If you did that then the latest file would be in $array[0] not $array[1].


Here's how to do it without calling an external program like ls:


opendir DIR, '.' or die "Cannot open '.' directory: $!";

my $latest = [ '', 0 ];

while ( my $file = readdir DIR ) {

my $mtime = ( stat $file )[ 9 ];

$latest = [ $file, $mtime ] if $latest->[ 1 ] < $mtime;
}

closedir DIR;

print "The latest file is $latest->[0].\n";

....or this. It will show all youngest files if there are more
than one:

use List::Util qw( min);

my %by_date;
push @{ $by_date{ -M $_} }, $_ for glob '* .*';
print "Youngest file(s): @$_\n" for $by_date{ min keys %by_date};

Anno
 
T

Tad McClellan

Tad McClellan said:
my @array = map { chomp; substr($_, 43) } qx/ ls -lt /;


It would probably be better to use something with a more consistent
date format than ls uses.

Doing it in native Perl would have a consistent date format, and
be more portable too.

Seems like the Schwartzian transform (perldoc -q sort) would
be the Right Tool here:


my @array = map { localtime($_->[1]) . " $_->[0]" }
sort { $b->[1] <=> $a->[1] }
map { [ $_, (stat)[9] ] } glob '*';
 
M

Mirco Wahab

Thus spoke Tad McClellan (on 2006-06-13 14:58):
Seems like the Schwartzian transform (perldoc -q sort) would
be the Right Tool here:


my @array = map { localtime($_->[1]) . " $_->[0]" }
sort { $b->[1] <=> $a->[1] }
map { [ $_, (stat)[9] ] } glob '*';

I tried to de-schwartzi-ficate this
and came up with the following:

my (%fn, $t);

$fn{$_} = [$t=(stat)[9], scalar localtime $t] for glob '*';

which can give the same results by:

print "$_ : $fn{$_}->[1]\n"
for
sort{ $fn{$b}->[0] <=> $fn{$a}->[0] }
keys %fn;


but I think, after all, the S.T. is more
appropriate here.

Regards

Mirco
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top