此命令使用 -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/