【问题标题】:Last modified date of a directory in OSXOSX 中目录的最后修改日期
【发布时间】:2012-04-17 19:36:15
【问题描述】:

如何在终端中获取目录的最后修改日期?

【问题讨论】:

  • 你能从你的目录中发布ls -ld . 的输出吗?不同版本的ls 在输出中使用不同的日期和时间格式
  • drwx-----@ 52 userid 员工 1768 Apr 17 11:25

标签: macos shell


【解决方案1】:

如果你只想获取修改日期(mtime)而不是别的

stat --printf='%y\n' directory_name

或者,对于自纪元以来的日期,以秒为单位:

stat --printf='%Y\n' directory_name

这比涉及ls/cut/grep/awk/find 等的解决方案更直接、高效和稳健

编辑

以上内容是在 OP 在下面的 cmets 中提到这是针对 OSX 之前发布的。

OP 使用stat/date 得出了一个解决方案,我同意该解决方案,所以我在此处添加它。

首先是stat

stat -f "%m" /path/test.app

获取目录的 mtime,然后将其包装在 date 中以获取所需格式

date -j -f "%s" "$(stat -f "%m" /path/test.app)" +"%Y/%m/%d %T"

【讨论】:

  • @user754905 什么操作系统?操作系统?试试--format=%y,或者看看man 1 stat
  • 如果我只做统计,我会得到:234881028 33275326 drwxr-xr-x 3 root staff 0 102 "Apr 17 12:02:42 2012" "Mar 22 15:05:11 2012" “2012 年 4 月 17 日 11:25:37”“2012 年 3 月 22 日 15:05:11”4096 0 0 /Users/userid/Library/test/test.app/
  • 是的,OSX。我基本上想得到这个“Mar 22 15:05:11 2012”或“Mar 22 2012”
  • 我没有 OSX,所以看看the man page 看看你能不能弄明白。我想它会像stat -f '%m' -t '%b %m %H:%M:%S %Y' dirname
  • 这就是我所做的: date -j -f "%s" "$(stat -f "%m" /path/test.app)" +"%Y/%m/% d %T"
【解决方案2】:
ls -lcdoq your_directory | awk '{print $5" "$6}'

【讨论】:

    【解决方案3】:

    递归:

    ls -Rlt | head -n 2 | cut -d ' ' -f10-12
    

    非递归:

    ls -lt | head -n 2 | cut -d ' ' -f10-12
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      • 1970-01-01
      相关资源
      最近更新 更多