To sort files by date reliably:
list_date_sorted_ascending() {
while IFS= read -r -d '' -u 9
do
printf '%q\0' "${REPLY#* }"
done 9< <(find "$1" -mindepth 1 -maxdepth 1 -printf '%T@' -exec printf ' %s\0' {} \; | sort --general-numeric-sort --zero-terminated)
}
To be able to use head
and tail
on NUL-separated output:
nul_terminated() {
tr '\0\n' '\n\0' | "$@" | tr '\0\n' '\n\0'
}
Putting it together:
tail -f "$(list_date_sorted_ascending /var/log | nul_terminated tail -n 1)"