Find and sort directories
I need to find directories not older than 30 days and then sort the开发者_运维知识库m by date (newest to oldest). This is my command:
find /tmp/logs/ -maxdepth 1 -mindepth 1 -type d -mtime -30
The problem is obviously the sorting part :)
If there are only "few" directories, you could pipe find
's output to xarg ls -t
, e.g.:
find /tmp/logs/ -maxdepth 1 -mindepth 1 -type d -mtime -30 | xargs ls -td1
Do your find from above, then use this at the end:
find -printf "%A@ %f\n" | sort -rn
That tells find to print the time (in seconds) and the filename. You can sort on the time and there you go. Pipe that into cut -d" " -f2-
to strip the time back off if you need to.
for f in $(find . -maxdepth 1 -mindepth 1 -type d -mtime -30)
do
echo -n $f " "
stat -c %Y $f
done
gives you the logs with modification dates. Sorting on the seconds should be easy.
精彩评论