Need help with UNIX command to get the latest file from list of files with timestamp


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
Need help with UNIX command to get the latest file from list of files with timestamp

Hi All,

I have list of files like below with name abcxyz.timestamp. I need a unix command to pick the latest file from the list of below files. Here in this case the lates file is abcxyz.20190304103200. I have used this unix command "ls abcxyz*|tail -1" but i heard that it is not the appropriate one which may not be successful all the time.I was told to use a command by sorting the files based on the timestamp. Please do the needful.

Code:
abcxyz.20190304103200
abcxyz.20190303131348
abcxyz.20190303115704
abcxyz.20190227153924
abcxyz.20190215171513
abcxyz.20190228123958

Thanks
Rakesh

Last edited by RavinderSingh13; 1 Week Ago at 02:45 AM..
# 2  
Old 1 Week Ago
That depends on what you call "latest". If you can make ABSOLUTELY sure the name's time stamp is correct, can't be tampered with, and coincides with the file creation, why not use it? If you don't, read man ls on the -t option.
This User Gave Thanks to RudiC For This Post:
rakeshp (6 Days Ago)
# 3  
Old 6 Days Ago
Using the filenames as the ONLY time reference:
Code:
newest=$( ls abcd*|  sort -n -k2 -t '.' | tail -1 )

Using file mtimes (last time written to, the default for ls -l )
Code:
newest=$( ls -rt '.' | tail -1 )

The inode time ls -c abcd* is another internal time kept. This is almost always the file creation time. File creation time (birth time) is not universal, older UNIXes filesystems do not support it.

Since your question vague this is about the best we can do for you. We need your shell and your OS (uname -a shows this) to do better.

Last edited by jim mcnamara; 6 Days Ago at 11:32 AM..
This User Gave Thanks to jim mcnamara For This Post:
rakeshp (6 Days Ago)
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Latest list of files of each type jhonnyrip Shell Programming and Scripting 3 07-08-2018 12:08 PM
UNIX command to get the latest file and also matching pattern vishwanath001 Shell Programming and Scripting 3 08-17-2016 05:45 AM
Picking the latest file based on a timestamp for a Dynamic file name chillblue Shell Programming and Scripting 4 06-20-2016 11:30 AM
I have this list of files . Now I will have to pick the latest file based on some condition LoneRanger Shell Programming and Scripting 5 07-24-2014 01:44 PM
Search for latest Timestamp in file netdbaind Shell Programming and Scripting 2 02-06-2013 11:28 PM
Sorting files based on timestamp and picking the latest file robertbrown624 UNIX for Dummies Questions & Answers 12 05-17-2012 10:41 PM
Urgent ...pls Sorting files based on timestamp and picking the latest file robertbrown624 Shell Programming and Scripting 1 05-17-2012 01:53 AM
How to pick only the latest files based on the timestamp? win4luv UNIX for Dummies Questions & Answers 43 06-29-2011 01:30 AM
List all old files except 15 latest files rockf1bull Red Hat 3 06-24-2011 07:42 AM
To list only the very latest files venkatesht UNIX for Dummies Questions & Answers 3 02-23-2009 11:26 AM
UNIX command: mv - Objective: move files based on timestamp HLee1981 UNIX for Dummies Questions & Answers 4 07-11-2008 01:02 PM
Unix shell scripting to find latest file having timestamp embedded... kaushik25 AIX 2 08-06-2007 10:42 PM
finding latest file having timestamp on it..... kaushik25 Shell Programming and Scripting 1 08-06-2007 10:35 PM
command unix to list all files created since n month ago yacsil Shell Programming and Scripting 8 12-15-2003 12:12 PM
ksh: How to get latest file from a list of files in a directory Jayathirtha Shell Programming and Scripting 1 06-24-2003 10:23 AM