jump to navigation

Sort recursively by date September 29, 2008

Posted by maxmil in : shell scripting , trackback

This little combination of commands sorts a list of files by descending date reading all the files recursively from the current folder
find . | xargs -i ls -l {} | sort -rk 6 | awk '$1 != "total" {print $0}'

Comments»

1. Nosey Parker - June 18, 2009

There are several problems with that.

– Files are listed twice: once because find passes their names to ls; secondly because ls expands directories when used this way.

– It will scale badly i.e. be very inefficient for large directories, because xargs starts a separate ls process for each file when xargs is used this way.

– It will sort incorrectly in some locales because the date format printed by ls is locale-dependent.

This solution prints all the files in reverse date/time order, which I think is closest to what was intended:
find . | xargs ls -ltd

This variation omits directories from the list:
find . ! -type d | xargs ls -ltd

This alternative lists files in each subdirectory in reverse date/time order, but groups files in each subdirectory together:
ls -ltR