Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2008
    Posts
    89

    Unanswered: Finding the file created last

    Hi everyone,

    I am trying to write a ksh script to automate a report.
    Part of that must be to find a file the name of which follows a certain pattern,
    which was the last one created (of all of those matching the pattern).

    Example:

    files in folder:
    Code:
    mama_dblog_grb_20081223.log
    papa_dblog_grb_20081120.log
    hans_dblog_grb_20081012.log
    mama_dblog_grb_20070324.log
    ... etc
    and i want to get the one that was created last, looking like "mama*.log", so i thought something like
    Code:
    ls -rt mama*.log
    only filtered down to the one file that was created last of all of them...

    I know it must be trivial for most, but I am fairly new in this shell game
    "My brain is just no good at being a relational Database - my relations suck real bad!"

  2. #2
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    Try something like:
    Code:
    file=`ls -rt1 | tail -1`
    echo "file is $file"
    • Your ls -rt command is fine but you really need to put one file per line to make it usefull so I used ls -rt1 in the above.
    • The tail -1 just keeps the last line.
    • The backticks are important as they run the commands before passing back the result to the variable file.
    • I don't have ksh on the machine infront of me but it should work.

    Mike

  3. #3
    Join Date
    Mar 2008
    Posts
    89

    It Does!

    Thanks mike_bike_kite, works perfectly the way I wanted it to!
    "My brain is just no good at being a relational Database - my relations suck real bad!"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •