getting last file from linux directory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bill

    getting last file from linux directory

    I have been presented with the task of getting the last file from
    a linux directory when the files are of the form:
    nnnnnn-xxxxxxx-.ext

    where nnnnnn are 6 numeric digits and xxxxxx is variable a/n data.

    I could read the entire directory in an sort, but I hope there is
    an easier way.

    suggestions ?

    bill
  • Toby A Inkster

    #2
    Re: getting last file from linux directory

    bill wrote:
    I could read the entire directory in an sort, but I hope there is
    an easier way.
    $dir = '/some/dir/';
    $filename = `ls -1r $dir | head -1`;
    # Note that $filename probably contains a line break
    # at the end. You might want to strip it out.

    is a quick and dirty way. Note this works in both PHP and Perl with no
    modifications! :-)

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me ~ http://tobyinkster.co.uk/contact
    Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

    * = I'm getting there!

    Comment

    • Gordon Burditt

      #3
      Re: getting last file from linux directory

      >I have been presented with the task of getting the last file from
      >a linux directory when the files are of the form:
      >nnnnnn-xxxxxxx-.ext
      >
      >where nnnnnn are 6 numeric digits and xxxxxx is variable a/n data.
      Define "last". Files do not have an intrinsic ordering.
      >I could read the entire directory in an sort, but I hope there is
      >an easier way.
      You may have to read the entire directory in, get the timestamp
      (which one? There are several.) for each file, and sort based on
      most recent timestamp.

      You also might want to avoid files that don't match the given filename
      pattern. Files with names matching *.core tend to appear even when
      not intended.

      Comment

      • bill

        #4
        Re: getting last file from linux directory

        Gordon Burditt wrote:
        >I have been presented with the task of getting the last file from
        >a linux directory when the files are of the form:
        >nnnnnn-xxxxxxx-.ext
        >>
        >where nnnnnn are 6 numeric digits and xxxxxx is variable a/n data.
        >
        Define "last". Files do not have an intrinsic ordering.
        >
        >I could read the entire directory in an sort, but I hope there is
        >an easier way.
        >
        You may have to read the entire directory in, get the timestamp
        (which one? There are several.) for each file, and sort based on
        most recent timestamp.
        >
        You also might want to avoid files that don't match the given filename
        pattern. Files with names matching *.core tend to appear even when
        not intended.
        >
        "last" is numerically highest nnnnnn

        bill

        Comment

        • bill

          #5
          Re: getting last file from linux directory

          Toby A Inkster wrote:
          bill wrote:
          >
          >I could read the entire directory in an sort, but I hope there is
          >an easier way.
          >
          $dir = '/some/dir/';
          $filename = `ls -1r $dir | head -1`;
          # Note that $filename probably contains a line break
          # at the end. You might want to strip it out.
          >
          is a quick and dirty way. Note this works in both PHP and Perl with no
          modifications! :-)
          >
          thanks

          bill

          Comment

          • Richard

            #6
            Re: getting last file from linux directory

            bill <nobody@spamcop .netwrites:
            Gordon Burditt wrote:
            >>I have been presented with the task of getting the last file from a
            >>linux directory when the files are of the form:
            >>nnnnnn-xxxxxxx-.ext
            >>>
            >>where nnnnnn are 6 numeric digits and xxxxxx is variable a/n data.
            >>
            >Define "last". Files do not have an intrinsic ordering.
            >>
            >>I could read the entire directory in an sort, but I hope there is
            >>an easier way.
            >>
            >You may have to read the entire directory in, get the timestamp
            >(which one? There are several.) for each file, and sort based on
            >most recent timestamp.
            >>
            >You also might want to avoid files that don't match the given filename
            >pattern. Files with names matching *.core tend to appear even when
            >not intended.
            >>
            "last" is numerically highest nnnnnn
            >
            bill
            So you have answered your own question. Read all files, filter ones with
            numeric prefixes and sort (ksort) based on the numeric part of the file
            name.

            Comment

            • Allodoxaphobia

              #7
              Re: getting last file from linux directory

              On Mon, 19 Feb 2007 06:54:01 -0500, bill wrote:
              Toby A Inkster wrote:
              >bill wrote:
              >>
              >>I could read the entire directory in an sort, but I hope there is
              >>an easier way.
              >>
              > $dir = '/some/dir/';
              > $filename = `ls -1r $dir | head -1`;
              > # Note that $filename probably contains a line break
              > # at the end. You might want to strip it out.
              >>
              >is a quick and dirty way. Note this works in both PHP and Perl with no
              >modification s! :-)
              >>
              >
              thanks
              Do a sanity check on the results. If ever an alpha-named file gets in
              that directory, it will appear first (`head -1`).

              Jonesy
              --
              Marvin L Jones | jonz | W3DHJ | linux
              38.24N 104.55W | @ config.com | Jonesy | OS/2
              *** Killfiling google posts: <http://jonz.net/ng.htm>

              Comment

              Working...