Here is a clean and robust way for sort | head by date:
Using ls -l for pretty print
find . ! -type d -printf "%T@ %p\0" |
sort -zrn |
head -zn 10 |
sed -z 's/^[0-9.]\+ //' |
xargs -0 ls -gt
As a bash function:
findByDate() {
local humansize=''
[ "$1" = "-h" ] && humansize='h' && shift
find . ${2:-! -type d} -printf "%T@ %p\0" |
sort -zrn |
head -zn ${1:--0} |
sed -z 's/^[0-9.]\+ //' |
xargs -0 ls -dgt${humansize}
}
This could by run with one or two argument, or even without:
Usage: findByDate [-h] [lines] [find options]
Sample:
findByDate
Will list all non directories sorted by date. Nota:
Even on big filesystem tree, as xargs recieve already sorted list, the file order stay correct, even if ls must be run many times.
findByDate -h 12
Will list 12 more recents non directories sorted by date, with size printed in human readable form
findByDate 42 '-type l'
Will list 42 more recents symlinks
findByDate -0 '( -type l -o -type b -o -type s -o -type c )'
Will list all symlinks, block devices, sockets and characters devices, sorted by date.
Inverting order
Replacing head by tail and change switch of sort and ls:
findByDate() {
local humansize=''
[ "$1" = "-h" ] && humansize='h' && shift
find . ${2:-! -type d} -printf "%T@ %p\0" |
sort -zn |
tail -zn ${1:-+0} |
sed -z 's/^[0-9.]\+ //' |
xargs -0 ls -dgtr${humansize}
}
Same function, same usage:
Usage: findByDate [-h] [lines] [find options]