Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-11-2012
Registered User
 
Join Date: Jun 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Finding latest dir based on it's name (yyyymmdd)

Hi Folks,
As part of my application I need to find out what the latest directory based
on the name of that directory (not it's file system timestamp).
Example: I have a directory which contains below directories (each of while contains files etc)
20120000/
20120000/
latest (symbolic link to 20120000)
So in this case the latest directory is latest and 20120000
If a new directory called 20130000 gets created here then that would be the latest directory.
If another new directory called 20090000 gets created then that would NOT be the latest directory
Is there a handy way (using grep / awk / find etc) to check the latest based on the "yyyymmdd" pattern
of the directory name.
Any advice would be great
Thanks - Ro
Sponsored Links
    #2  
Old 06-11-2012
bartus11's Avatar
Registered User
 
Join Date: Apr 2009
Posts: 3,139
Thanks: 3
Thanked 955 Times in 934 Posts

Code:
ls | sort -r | grep -v latest | head -1

Sponsored Links
    #3  
Old 06-11-2012
Registered User
 
Join Date: Jun 2007
Location: North-West UK
Posts: 521
Thanks: 73
Thanked 85 Times in 81 Posts
Would a simple sort suffice?

Code:
ls -1|sort -n|tail -1


I hope that this helps. If not, then I'm not understanding what you actually want. Please elaborate, with listings if you have any.



Robin
Liverpool/Blackburn
UK

---------- Post updated at 03:20 PM ---------- Previous update was at 03:19 PM ----------

In fact, if you are looking for the highest number, then just

Code:
ls -1|tail -1

should work.
    #4  
Old 06-11-2012
methyl methyl is offline Forum Staff  
Moderator
 
Join Date: Mar 2008
Posts: 6,388
Thanks: 286
Thanked 668 Times in 640 Posts
I think we need to stop ls finding the directory called "latest".

Code:
ls -1d ????0000 | tail -1

Sponsored Links
    #5  
Old 06-11-2012
Registered User
 
Join Date: Jun 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Hey Folks,

Cheers, the below worked a treat for me:

ls | sort -r | grep -v latest | head -1

Thanks!

Ronan
Sponsored Links
    #6  
Old 09-28-2012
Registered User
 
Join Date: Sep 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
May be, you want to put the result into a variable:
lastdirectory=`ls | sort -r | grep -v latest | head -1`
echo $lastdirectory
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to pick only the latest files based on the timestamp? win4luv UNIX for Dummies Questions & Answers 43 06-29-2011 01:30 AM
Finding the Latest file in a Dir filter Shell Programming and Scripting 2 06-24-2011 11:54 AM
grep latest file based on date. lweegp Shell Programming and Scripting 12 04-03-2009 03:10 AM
finding latest file having timestamp on it..... kaushik25 Shell Programming and Scripting 1 08-06-2007 10:35 PM
finding latest file in Unix nick12 UNIX for Dummies Questions & Answers 2 03-05-2005 01:27 AM



All times are GMT -4. The time now is 05:11 AM.