按日期对查找命令进行排序

and*_*mrg 2 linux bash ls find

我有一个脚本:

newerthan="2016-02-08"
olderthan="2016-04-29"
find / -type f -name "*" -newermt "$newerthan" ! -newermt "$olderthan" -ls
Run Code Online (Sandbox Code Playgroud)

该列表文件:

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
Run Code Online (Sandbox Code Playgroud)

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

Rav*_*ezu 6

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

find / -type f -name "*" -newermt "$newerthan" ! -newermt "$olderthan" -printf "%T+\t%p\n" | sort
Run Code Online (Sandbox Code Playgroud)

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

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

  • 如果你想颠倒顺序:使用“sort -r”而不是“sort”。 (2认同)
  • macOS 用户将需要 `brew install findutils` 并将 `find` 替换为 `gfind`,因为 Apple 版本的 find 不支持 -printf。 (2认同)