The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script to find latest executable in particular directory and start that particular ex kvineeth Shell Programming and Scripting 6 09-24-2007 08:19 AM
Unix shell scripting to find latest file having timestamp embedded... kaushik25 AIX 2 08-06-2007 11:42 PM
Find and remove all but the latest file hyennah Shell Programming and Scripting 4 03-29-2007 12:09 AM
finding latest file in Unix nick12 UNIX for Dummies Questions & Answers 2 03-05-2005 02:27 AM
how to find a file in UNIX without find command? bluo Shell Programming and Scripting 3 09-25-2003 12:47 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-03-2006
duke0001 duke0001 is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 131
How to find the latest file on Unix or Linux

Please help me out how to identify the latest file in one directory by looking at file's timestamp or datestamp. You can say using system command. Thanks
  #2 (permalink)  
Old 11-03-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,802

Code:
cd directory
ls -l -rt | head -1

is one way.
  #3 (permalink)  
Old 11-03-2006
lazytech lazytech is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 39
Quote:
Originally Posted by jim mcnamara
Code:
cd directory
ls -l -rt | head -1

is one way.
You would use tail instead of head for the latest file.
  #4 (permalink)  
Old 11-03-2006
duke0001 duke0001 is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 131
Jim, Lazytech:

Thanks very much for your advice. I mean the latest file by timestamp or datestamp. Using head -l or tail -l only list file in the begining or in the end. I don't care the order of the file listing. I want to know how to get or find the latest file based on date or time. Thanks.
  #5 (permalink)  
Old 11-04-2006
BOFH BOFH is offline Forum Advisor  
Registered User
  
 

Join Date: Feb 2005
Location: Broomfield, CO
Posts: 406
Well, if you man ls, you'll see what the switches mean. The -t switch sorts the listing by 'time' so the output is displayed from newest to oldest. The -r switch reverses it so the newest file is at the end of the listing instead of the beginning. And -l gives you the long listing.


Code:
$ ls -lrt
total 178880
-rw-r--r--  1 carlsche  carlsche      2727 13 Jul  2005 badlist
-rwxr-xr-x  1 carlsche  carlsche      1076 13 Jul  2005 scanlist
-rw-r--r--  1 carlsche  carlsche   1428173 22 May 21:25 dirlist.album
-rwxr-xr-x  1 carlsche  carlsche       429 22 May 21:26 dirout
-rw-r--r--  1 carlsche  carlsche   1404661 22 May 21:32 dirlist.song
-rw-r--r--  1 carlsche  carlsche   1404035 22 May 21:33 dirlist.artist
drwxr-xr-x  6 carlsche  carlsche       204 24 Sep 22:18 Previous iTunes Libraries
drwxr-xr-x  3 carlsche  carlsche       102 24 Sep 22:19 Album Artwork
drwxr-xr-x  9 carlsche  carlsche       306 24 Sep 22:40 iTunes Music
-rw-r--r--  1 carlsche  carlsche  42887486 29 Oct 18:32 iTunes Library
-rw-r--r--  1 carlsche  carlsche  44442517 29 Oct 18:32 iTunes Music Library.xml

Carl
  #6 (permalink)  
Old 11-06-2006
duke0001 duke0001 is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 131
Carl:

Thanks for your explanation. It is clear. Question is: if I want to find the latest file (if reversed, it will be last file) to re-name and move to another directory on Linux. What kind of code should I write? Since using ls -lrt |tail -l, it will show a list of the latest file, not the last file. May I write code like this:

ls -lrt |tail -l |mv /directory1/file1 /directory2/file2

Please advise. Thank you so much.
  #7 (permalink)  
Old 11-04-2008
udaylingamaneni udaylingamaneni is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 3
Try below
> mv `find ./ -maxdepth 1 -atime 10 | tail -1` new_file_name

But this will work for files only in current directory and only if there are files that are accessed in last 10 days(you can change the velue)
[I am a newbie . excuse me if there is anything wrong]

Any way [ $ mv `ls -t | head -1` /temp/newfile_name ] by BOFH is straight forward
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:01 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0