Sort logs by date field in bash

后端 未结 4 1971
我寻月下人不归
我寻月下人不归 2020-12-05 04:43

let\'s have

126 Mar  8 07:45:09 nod1 /sbin/ccccilio[12712]: INFO: sadasdasdas
  2 Mar  9 08:16:22 nod1 /sbin/zzzzo[12712]: sadsdasdas
  1 Mar  8 17:20:01 no         


        
4条回答
  •  臣服心动
    2020-12-05 05:22

    little off-topic - but anyway. only useful when working within filetrees

    ls -l -r --sort=time
    

    from this you could create a one-liner which for example deletes the oldest backup in town.

    ls -l -r --sort=time | grep backup | head -n1 | while read line; do oldbackup=\`echo $line | awk '{print$8}'\`; rm $oldbackup; done;
    

提交回复
热议问题