linux - 使用 find 命令格式化时间戳

标签 linux date find printf

此命令使用 -printf '%Tc %p\n' 给出文件最后修改的日期。

$ find ./ -daystart -mtime -3 -type f  ! -mtime -1  -printf '%Tc %p\n'
Tue 08 Mar 2016 12:25:01 NZDT ./compareKPIs-log
Tue 08 Mar 2016 18:04:58 NZDT ./backup_public_html_20160308.tgz
Tue 08 Mar 2016 18:04:58 NZDT ./log-file
Tue 08 Mar 2016 12:25:01 NZDT ./compareKPIs-error
Mon 07 Mar 2016 18:05:02 NZDT ./backup_public_html_20160307.tgz

我想做的是控制日期格式为YYYY-mm-dd。我该怎么做呢? (hh:mm:ss 也可以在这里加上)

例如 -printf '%TY %p\n' 会给我年份%TY:

$ find ./ -daystart -mtime -3 -type f  ! -mtime -1  -printf '%TY %p\n'
2016 ./compareKPIs-log
2016 ./backup_public_html_20160308.tgz
2016 ./log-file
2016 ./compareKPIs-error
2016 ./backup_public_html_20160307.tgz

例如 -printf '%Tm %p\n' 会给我月份%Tm:

$ find ./ -daystart -mtime -3 -type f  ! -mtime -1  -printf '%Tm %p\n'
03 ./compareKPIs-log
03 ./backup_public_html_20160308.tgz
03 ./log-file
03 ./compareKPIs-error
03 ./backup_public_html_20160307.tgz

另一个optio n 是 ls -exec ls -ld {}\; 但我不确定这种方式是否可行。

$ find ./ -daystart -mtime -3 -type f  ! -mtime -1 -exec ls -ld {} \;
-rw-rw-r-- 1 kevinsmith kevinsmith 254160 Mar  8 12:25 ./compareKPIs-log
-rw-rw-r-- 1 kevinsmith kevinsmith 956411793 Mar  8 18:04 ./backup_public_html_20160308.tgz
-rw-rw-r-- 1 kevinsmith kevinsmith 78577523 Mar  8 18:04 ./log-file
-rw-rw-r-- 1 kevinsmith kevinsmith 46080 Mar  8 12:25 ./compareKPIs-error
-rw-rw-r-- 1 kevinsmith kevinsmith 956407500 Mar  7 18:05 ./backup_public_html_20160307.tgz

最佳答案

你可以在find-printf参数中使用多个%Tk,例如:

  • '%TY-%Tm-%Td %p\n' YYYY-mm-dd [文件] 格式
  • '%TY-%Tm-%Td %TH:%TM:%.2TS %p\n' for YYYY-mm-dd hh:mm:ss [文件] 格式
  • '%TY-%Tm-%Td %TH:%TM:%TS %p\n' for YYYY-mm-dd hh:mm:ss.ssssssssss [文件] 格式

关于linux - 使用 find 命令格式化时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35878134/

相关文章:

phpMyAdmin - 配置文件是否更改?

javascript - setFullYear 不设置 Date 对象的小时/分钟/秒

python - 在 python 中解析法语日期

Excel/SQLite 日期序列号

arrays - 如何比较两个数组,删除相似的项目,而不迭代整个数组?

jquery、选择器、样式

linux - 如何使用 brewperl 在 Ubuntu 上安装应用程序

linux - 为什么这个 unix shell 代码会这样工作?

bash - "find"当前目录下的文件但跳过了一些特定的子目录

c - 从 "project root"运行脚本的正确方法是什么?