【问题标题】:Sort a find command by date按日期对查找命令进行排序
【发布时间】:2016-08-24 21:48:29
【问题描述】:

我有一个脚本:

newerthan="2016-02-08"
olderthan="2016-04-29"
find / -type f -name "*" -newermt "$newerthan" ! -newermt "$olderthan" -ls

此列表文件:

16481    0 -r--r--r--   1 root     root         4096 Mar 16 11:41 /sys/module/sunrpc/srcversion
 16482    0 -r--r--r--   1 root     root         4096 Mar 13 04:42 /sys/module/sunrpc/initstate
 16483    0 -r--r--r--   1 root     root         4096 Mar 16 11:41 /sys/module/sunrpc/refcnt
 16485    0 -r--r--r--   1 root     root         4096 Mar 17 11:41 /sys/module/sunrpc/sections/.note.gnu.build-id
 16486    0 -r--r--r--   1 root     root         4096 Mar 12 11:41 /sys/module/sunrpc/sections/.text

是否可以按日期、结果排序?

【问题讨论】:

标签: linux bash find ls


【解决方案1】:

如果您想按排序顺序打印文件名和日期:

find / -type f -name "*" -newermt "$newerthan" ! -newermt "$olderthan" -printf "%T+\t%p\n" | sort

如果您只想按排序顺序打印文件名:

find / -type f -name "*" -newermt "$newerthan" ! -newermt "$olderthan" -printf "%T+\t%p\n" | sort | awk '{print $2}'

【讨论】:

  • 如果你想颠倒顺序:使用sort -r而不是sort
【解决方案2】:

这将返回一个日期排序、时间戳限制、显示文件名的列表

find / -type f -name "*" -newermt "$newerthan" ! -newermt "$olderthan"  -printf '%T@ %p\n' | sort -k 1 -n | sed 's/^[^ ]* //'

如果您需要其他列,可以更改 printf 和 sort 参数以显示更多列并按位置而不是按列排序

【讨论】:

    猜你喜欢
    • 2020-10-30
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 2021-11-03
    相关资源
    最近更新 更多