Reputation: 7
i have to find the latest version of a file in subdirectories
user1@host1:/tmp/pathtofind> find . -name "*.txt" -printf "%T+ %p\n"
2013-11-21+13:34:05.7255244000 ./20131119/abc.txt
2013-11-21+13:33:56.5965250740 ./20131120/abc.txt
2013-11-21+13:34:17.6735259520 ./20131118/abc.txt
2013-11-21+13:33:53.0055261810 ./20131121/abc.txt
All I need is the path and filename of the latest version of the file for further processing, in this case it would be ./20131118/abc.txt
Can you please help me how to do this?
Regards, vishal
EDIT:
Sorry what I actually need is the last version of every distinct filename (abc.txt and def.txt) in a directory:
user1@host1:/tmp/pathtofind$ find . -name "*.txt" -printf "%T+ %p\n"
2013-11-21+17:56:48.4251785260 ./20131121/abc.txt
2013-11-21+17:56:57.8651782010 ./20131121/def.txt
2013-11-21+17:56:37.6731789030 ./20131118/abc.txt
2013-11-21+17:57:14.2571776330 ./20131118/def.txt
2013-11-21+17:56:24.2011793670 ./20131119/abc.txt
2013-11-21+17:57:24.4011772720 ./20131119/def.txt
2013-11-21+17:56:29.1371791980 ./20131120/abc.txt
2013-11-21+17:57:19.6411774490 ./20131120/def.txt
user1@host1:/tmp/pathtofind$ find . -name "*.txt" -printf "%T+ %p\n" | sort | tail -1
2013-11-21+17:57:24.4011772720 ./20131119/def.txt
only finds the latest version of a textfile.
what i actually need in this case would be:
2013-11-21+17:56:48.4251785260 ./20131121/abc.txt
2013-11-21+17:57:24.4011772720 ./20131119/def.txt
regards, vishal
Upvotes: 0
Views: 1778
Reputation: 19395
Pipe the above output to
sort -k1.43 -k1r|uniq -s42
- this sorts by filename (which starts at column 43) and then reverse by timestamp (so that the latest version comes first) and prints the first occurrence of each filename.
Upvotes: 0
Reputation: 123658
Change the printf
format to emit the seconds since epoch and sort. Saying:
find . -name "*.txt" -printf "%T@ %p\n" | sort | tail -1
would print the newest file.
-printf format
...
@ seconds since Jan. 1, 1970, 00:00 GMT, with frac‐
tional part.
Upvotes: 1