4 ms·
Most of the time I avoid parsing ls, but I haven't found a reliable way to do this one: latest="$(ls -1 $pattern | sort --reverse --version-sort | head -1)"
by tremon 2y ago
Most of the time I avoid parsing ls, but I haven't found a reliable way to do this one:
latest="$(ls -1 $pattern | sort --reverse --version-sort | head -1)"
Anyone got a better solution?
- chenxiaolong 2y agoThis should work with any arbitrary filename: latest=$(printf '%s\0' <glob> | sort -zrV | head -zn1) or with long args: latest=$(printf '%s\0' <glob> | sort --zero-terminated --reverse --version-sort | head --zero-terminated --lines 1
- genrilz 2y agoWhat unix is this on? Neither the mac nor gnu manpages have a -z or --zero-terminated option for head.
- tremon 2y agoDebian's head (from GNU coreutils) does: https://manpages.debian.org/bookworm/coreutils/head.1.en.html https://manpages.debian.org/bookworm/coreutils/head.1.en.htm...
- genrilz 2y agoYay! Glad to see zero termination flags in more places. EDIT: The linux manpages I read were from die.net, which it looks like were from 2010, guess I'll have to avoid them in the future. I checked FreeBSD, OpenBSD, and Mac man page to make sure, and unfortunately none of them support the -z flag yet.
- genrilz 2y agoThis ones a hard one. Since "--version-sort" isn't standard anyways, lets assume we can use flags which are common to BSD and GNU. Furthermore, lets assume bash or zsh so we can use "read -d ''". In that case, how about: IFS='' read -d '' latest < <(find $pattern -prune -print0 | sort -z --reverse --version-sort)