Modifying ls

Discussion in 'Perl Misc' started by manian.k@gmail.com, Jun 12, 2006.

  1. Guest

    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
     
    , Jun 12, 2006
    #1
    1. Advertising

  2. wrote:
    > 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
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jun 12, 2006
    #2
    1. Advertising

  3. <> wrote:

    > 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 /;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 12, 2006
    #3
  4. wrote:
    > 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]" );

    --
    Charles DeRykus
     
    Charles DeRykus, Jun 13, 2006
    #4
  5. Bart Lateur Guest

    wrote:

    >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.

    --
    Bart.
     
    Bart Lateur, Jun 13, 2006
    #5
  6. Anno Siegel Guest

    John W. Krahn wrote:

    > 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
     
    Anno Siegel, Jun 13, 2006
    #6
  7. Tad McClellan <> wrote:
    > <> wrote:
    >
    >> 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 /;



    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 '*';


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 13, 2006
    #7
  8. Mirco Wahab Guest

    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
     
    Mirco Wahab, Jun 13, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shawn Zabel

    Perl & XS -- Modifying a char*

    Shawn Zabel, Jul 19, 2004, in forum: Perl
    Replies:
    0
    Views:
    525
    Shawn Zabel
    Jul 19, 2004
  2. ladygrinningsoul

    Modifying a file's time stamp

    ladygrinningsoul, Dec 5, 2004, in forum: Perl
    Replies:
    1
    Views:
    556
    ladygrinningsoul
    Dec 6, 2004
  3. Brahmam
    Replies:
    3
    Views:
    561
    Francois Beaussier
    Jan 11, 2006
  4. Peter D. Dunlap

    Modifying the Back Button Function

    Peter D. Dunlap, Jul 3, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    437
    Cowboy \(Gregory A. Beamer\)
    Jul 3, 2003
  5. AspDotNetDeveloper

    Modifying output in Repeater

    AspDotNetDeveloper, Jul 17, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    355
    AspDotNetDeveloper
    Jul 17, 2003
Loading...

Share This Page