[Solved] Finding the latest file in a directory | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


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 !!

[Solved] Finding the latest file in a directory

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-15-2012
abhi_123 abhi_123 is offline
Registered User
 
Join Date: Jun 2008
Last Activity: 26 June 2014, 12:49 AM EDT
Posts: 39
Thanks: 2
Thanked 0 Times in 0 Posts
[Solved] Finding the latest file in a directory

Hi All,

I am using the below command to find the latest file in a dir:

Code:
ls -tr $v_sftphomedir/$v_sourcefile |tail -1

or

Code:
ls -t1 $v_sftphomedir/$v_sourcefile |head -1

and the outpur returned is below:

Code:
/home/cobr_sftp/var/controllingload/Backup/Dbrwds_Div_1796050246.txt

I need only the filename(Dbrwds_Div_1796050246.txt) and not path. Who do i fetch only the filename from it?

Please help

Moderator's Comments:
Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by abhi_123; 11-15-2012 at 06:50 AM..
Sponsored Links
    #2  
Old 11-15-2012
birei birei is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 24 June 2014, 5:36 AM EDT
Location: Bilbao, Spain
Posts: 628
Thanks: 8
Thanked 173 Times in 171 Posts
Hi abhi_123,

Use sed. Add it at the end of the pipe chain:

Code:
... | sed -e 's|^.*/||'

Sponsored Links
    #3  
Old 11-15-2012
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 27 July 2014, 5:28 AM EDT
Location: Switzerland - GE
Posts: 5,609
Thanks: 151
Thanked 387 Times in 364 Posts
What OS are you using since your output is not what I would expect?
IMHO using ls without -l should not give the absolute path...
    #4  
Old 11-15-2012
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 28 July 2014, 2:13 AM EDT
Location: San Jose, CA, USA
Posts: 4,158
Thanks: 162
Thanked 1,417 Times in 1,202 Posts
A simpler ways include:

Code:
basename "$v_sourcefile"

and when using POSIX compliant shells

Code:
${v_sourcefile##*/}

---------- Post updated at 08:08 ---------- Previous update was at 07:58 ----------

Quote:
Originally Posted by vbe View Post
What OS are you using since your output is not what I would expect?
IMHO using ls without -l should not give the absolute path...
If $v_sftphomedir/$v_sourcefile names a regular file (not a directory), then ls (no matter what options are given) will definitely print the full pathname of the file. If $v_sourcefile is named appropriately, it should name a single file; not a directory. Note that in this case the |tail -1 and |head -1 , respectively, in the code examples given in the 1st message in this thread were no-ops.
The Following User Says Thank You to Don Cragun For This Useful Post:
vbe (11-15-2012)
Sponsored Links
    #5  
Old 11-15-2012
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 27 July 2014, 5:28 AM EDT
Location: Switzerland - GE
Posts: 5,609
Thanks: 151
Thanked 387 Times in 364 Posts
Quote:
If $v_sftphomedir/$v_sourcefile names a regular file (not a directory)
Exact but the title was:
Quote:
Finding the latest file in a directory
and if it were a file then the -t is useless...
Sponsored Links
    #6  
Old 11-16-2012
abhi_123 abhi_123 is offline
Registered User
 
Join Date: Jun 2008
Last Activity: 26 June 2014, 12:49 AM EDT
Posts: 39
Thanks: 2
Thanked 0 Times in 0 Posts
thanks it worked

thanks all it worked.
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
Finding the Latest file in a Dir filter Shell Programming and Scripting 2 06-24-2011 11:54 AM
Move the latest or older File from one directory to another Directory pp_ayyanar Shell Programming and Scripting 1 03-24-2010 03:22 PM
Copy the latest file from one directory to another chandancsc Shell Programming and Scripting 3 08-07-2009 04:35 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 02:14 AM.