2016-03-08 107 views
0

此命令使用-printf '%Tc %p\n'爲上次修改的文件提供日期。使用find命令格式化時間戳

$ 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 

回答

1

可以使用多於一個的%T K的-printffind參數,例如:

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