I've two files FileA and FileB. Can someone please let me know how to get time for latest created file in a folder in Unix?
Asked
Active
Viewed 935 times
1 Answers
1
Both for only two files and the general case of n files, you can use find
:
find -type f -printf '%T@ \n' | sort -n | tail -1
If the files need to match a pattern, you can use something like:
find -type f -name 'example*.txt' -printf '%T@ \n' | sort -n | tail -1
This prints all modification times of files in the working directory, sorts them, then selects the last (largest) one.

Michael Jaros
- 4,586
- 1
- 22
- 39
-
When I execute first command it gave belwo output. I'm not sure what it means :( Please let me know. find -type f -printf '%T@ \n' | sort -n | tail -1 1427994119.6287518250 – Drools123 Apr 02 '15 at 17:44
-
That is the largest modification time as [Unix time](http://en.wikipedia.org/wiki/Unix_time). If you need a human readable format, you can use for example `%T+` instead of `%T@`. – Michael Jaros Apr 02 '15 at 18:00
-
find -printf '%T+ \n' | sort -n | tail -1 2015-04-02+18:25:48.8187518250 --> Can we get milliseconds just up to three digits? Something like 18:25:48.819 – Drools123 Apr 02 '15 at 18:44
-
@Unixuser: You can't specify that precision with `find`, but you could pipe through `sed`: Just add `| sed -re 's/(\.[0-9]{3})[0-9]+/\1/'` – Michael Jaros Apr 02 '15 at 20:36
-
It worked :) Thank you. I don't want to bug you more. Last question, can you please tell me if there is any way that we can get the format as 2015-04-03.13:09:44:427 Now we're getting + before 13 and . before 427 , I need to replace these two. – Drools123 Apr 03 '15 at 13:16
-
If you look into the `find(1)` manpage and search for `%A`, it gives you all time and date modifiers. You could for example replace `%T+` resp. `%T@` with something like `%TY-%Tm-%Td. %TH:%TM:%TS` to match the example you gave (if you also add the millisecond fix mentioned above). – Michael Jaros Apr 03 '15 at 13:29