3 ms·
This should work with any arbitrary filename: latest=$(printf '%s\0' <glob> | sort -zrV | head -zn1) or with long args: latest=$(printf '%s\0' <glob>
by chenxiaolong 2y ago
This 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.