If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Finding the file created last

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-23-08, 09:28
freeBatjko freeBatjko is offline
Registered User
 
Join Date: Mar 2008
Posts: 89
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!"
Reply With Quote
  #2 (permalink)  
Old 12-23-08, 11:18
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
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
Reply With Quote
  #3 (permalink)  
Old 12-24-08, 05:46
freeBatjko freeBatjko is offline
Registered User
 
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!"
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On