Skip to main content
13 events
when toggle format what by license comment
Jul 13 at 16:02 comment added Timo @user, how can I invoke a cmd, e.g. unzip on the newest found file? I use -exec unzip{} which is not sorted by newest.
Jan 31 at 2:47 comment added Tom Hale This breaks on filenames that include \n. Here is a safe version.
Nov 12, 2022 at 6:27 comment added michael it probably goes without saying, but imho find is in many ways (for this specific question) preferable to ls, and can be used instead of ls in many of the other answers provided. But in this case, one would prefer not to have a recursive file listing. So to just list files in the current directory, add in find . -maxdepth 1 -type f .... etc etc
Sep 23, 2022 at 14:56 comment added Mikhail T. The sort, cut, and tail combination can be replaced with a single awk looking for the maximum number -- then outputting the second field at the end. An O(n) operation rather than O(n*ln(n)), that sort requires: awk '$1 > latest { latest = $1; p = $2 } END { print p }'
Nov 8, 2021 at 14:37 history edited Trevor Boyd Smith CC BY-SA 4.0
added english explanation of the command line because the `-f 2-` comment was not clear what the function/purpose was.
Jun 10, 2021 at 17:42 comment added balu IMO this is the most useful of all the answers here. Thank you!
Dec 11, 2019 at 21:21 comment added yoz @user's Mac version only sorts/displays date, not time. Here's a fixed version: find $DIR -type f -exec stat -lt "%F %T" {} \+ | cut -d' ' -f6- | sort -n | tail -1
Jul 27, 2017 at 8:33 comment added Simon Very useful! I find it more friendly if piped to ls though: find ./ -type f -printf "%T@ %p\n" -ls | sort -n | cut -d' ' -f 2- | tail -n 1 | xargs -r ls -lah
May 14, 2017 at 11:48 comment added Dennis Estenson As in the suggestion by chaos, if you reverse the sort by using sort -nr, you can use head -n 1 instead of tail -n 1 and improve efficiency slightly. (Although if you're sorting a recursive find, getting the first or last line won't be the slow part.)
Jan 11, 2017 at 21:29 history edited gioele CC BY-SA 3.0
updated to follow Kevin's suggestion
Jan 11, 2017 at 15:02 comment added Kevin Problem is the cut command truncates paths with spaces instead of -f 2 use -f2- to return fields 2 to the end of the line
Oct 12, 2015 at 20:32 comment added user Here's a solution for Mac: find $DIR -type f -exec stat -lt "%Y-%m-%d" {} \+ | cut -d' ' -f6- | sort -n | tail -1
Apr 12, 2014 at 18:15 history answered gioele CC BY-SA 3.0