Your subject says that you want to use awk, which your code already does, and it works for me. You haven't indicated what the problem is?

Although you could just use the code you have, I'm guessing your question is how to do this in Perl.

Piece 1: List files with glob (there are other ways, such as readdir or Path::Tiny, but glob is good for simple tasks), e.g. glob('/tmp/dir1/d1*')

Piece 2: Get the size of a file with -s, e.g. -s $filename

Piece 3: Using an array of arrays to hold the filenames and sizes (see also perlreftut)

Piece 4: Sort numerically with sort, e.g. sort {$b<=>$a} @filesizes

Putting it all together:

my @filenames = glob '/tmp/dir1/d1*'; my @files_and_sizes; for my $filename (@filenames) { my $filesize = -s $filename; push @files_and_sizes, [$filename, $filesize]; } use Data::Dumper; # just for demo & debugging print Dumper \@files_and_sizes; # just for demo & debugging @files_and_sizes = sort { $$b[1] <=> $$a[1] } @files_and_sizes; print Dumper \@files_and_sizes; # just for demo & debugging my $var1 = $files_and_sizes[0][0]; print $var1;

This could even be dramatically shortened into a one-liner, esp. if you replace the for with map. There are also several other ways to do this, e.g. with modules.


In reply to Re: find biggest file and use awk by Anonymous Monk
in thread find biggest file and use awk by mxtime

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.