Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find the newest file in a directure structure 1

Status
Not open for further replies.

Larshg

Programmer
Mar 1, 2001
187
DK
Hi

I need to have a command that will find the newest file in a directure structure

ex.
I have 2 directories where their are some file in
/test1
-rw-r--r-- 1 intecmd users 184525 Feb 24 16:10 /datafiles/comverse_smsc/COMV09A/bill.20040224151500
-rw-r--r-- 1 intecmd users 201517 Feb 24 16:10 /datafiles/comverse_smsc/COMV09A/bill.20040224153000
-rw-r--r-- 1 intecmd users 206021 Feb 24 16:10 /datafiles/comverse_smsc/COMV09A/bill.20040224154500
-rw-r--r-- 1 intecmd users 235080 Feb 24 16:40 /datafiles/comverse_smsc/COMV09A/bill.20040224160000
/test2
-rw-r--r-- 1 intecmd users 102829 Feb 19 00:25 /datafiles/comverse_smsc/COMV09B/20040218/bill.20040218233000
-rw-r--r-- 1 intecmd users 68499 Feb 19 00:25 /datafiles/comverse_smsc/COMV09B/20040218/bill.20040218234500
-rw-r--r-- 1 intecmd users 38847 Feb 19 00:40 /datafiles/comverse_smsc/COMV09B/20040219/bill.20040219000000
-rw-r--r-- 1 intecmd users 13391 Feb 19 00:40 /datafiles/comverse_smsc/COMV09B/20040219/bill.20040219000912
-rw-r--r-- 1 intecmd users 112 Feb 19 00:40 /datafiles/comverse_smsc/COMV09B/20040219/bill.20040219001329
-rw-r--r-- 1 intecmd users 51480 Feb 19 01:10 /datafiles/comverse_smsc/COMV09B/20040219/bill.20040219001500
-rw-r--r-- 1 intecmd users 5621 Feb 19 01:10 /datafiles/comverse_smsc/COMV09B/20040219/bill.20040219003000




I need a command that will always return the newest file in this case
-rw-r--r-- 1 intecmd users 235080 Feb 24 16:40 /datafiles/comverse_smsc/COMV09A/bill.20040224160000

I've tryede using find - but I cant get it to sort the files in different directories.


Regards Larshg
 
ls -1altr| tail -1

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
In ksh, you can try something like this:
Code:
t1=/test1/$(ls -t /test1 | head -1)
t2=/test2/$(ls -t /test2 | head -1)
[[ &quot;$t1&quot; -nt &quot;$t2&quot; ]] &amp;&amp; echo $t1 || echo $t2

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
[elephant2] A solution if directories contains sub-directories ...

[tt]
#!/usr/bin/sh
# Script file : newest_file
# Arg(s) : Directories to scan
#
tar cf - ${*:-.} 2&gt;/dev/null | tar tvf - | awk '
BEGIN {
Fmonth = 5;
Fday = 6;
Ftime = 7;
Fyear = 8;
m[&quot;Jan&quot;] = &quot;01&quot;;
m[&quot;Feb&quot;] = &quot;02&quot;;
m[&quot;Mar&quot;] = &quot;03&quot;;
m[&quot;Apr&quot;] = &quot;04&quot;;
m[&quot;May&quot;] = &quot;05&quot;;
m[&quot;Jun&quot;] = &quot;06&quot;;
m[&quot;Jul&quot;] = &quot;07&quot;;
m[&quot;Aug&quot;] = &quot;08&quot;;
m[&quot;Sep&quot;] = &quot;09&quot;;
m[&quot;Oct&quot;] = &quot;10&quot;;
m[&quot;Nov&quot;] = &quot;11&quot;;
m[&quot;Dec&quot;] = &quot;12&quot;;
}
$0 !~ /^d/ {
time = $Ftime;
gsub(/:/,&quot;&quot;,time);
timestamp = $Fyear m[$Fmonth] $Fday time;
print timestamp,$0;
} ' | sort -k1,1rn | head -1 | cut -d' ' -f2-
#--- End of script

newest_files /test1 /test2
[/tt]



Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top