我是Linux(Fedora-20)的新手,我正在努力学习基础知识,我有以下命令
echo "`stat -c "The file "%n" was modified on ""%y" *Des*`"这个命令返回这个输出
The file Desktop was modified on 2014-11-01 18:23:29.410148517 +0000我想把它格式化如下:
The file Desktop was modified on 2014-11-01 at 18:23我该怎么做?
发布于 2014-11-01 21:50:00
您不能在stat中真正做到这一点(除非您有一个我不知道的智能版本的stat )。
用date
很可能,您的date足够聪明,并处理-r开关。
date -r Desktop +"The file Desktop was modified on %F at %R"由于您的glob,您需要一个循环来处理与*Des*匹配的所有文件(在Bash中):
shopt -s nullglob
for file in *Des*; do
date -r "$file" +"The file ${file//%/%%} was modified on %F at %R"
done用find
很可能您的find有一个丰富的-printf选项:
find . -maxdepth 1 -name '*Des*' -printf 'The file %f was modified on %TY-%Tm-%Td at %TH:%TM\n'我想使用stat
(因为您的date不处理-r开关,所以您不想使用find,或者仅仅因为您喜欢使用尽可能多的工具来给您的妹妹留下深刻印象)。在这种情况下,最安全的做法是:
date -d "@$(stat -c '%Y' Desktop)" +"The file Desktop was modified on %F at %R"还有你的口香糖要求(在巴什):
shopt -s nullglob
for file in *Des*; do
date -d "@$(stat -c '%Y' -- "$file")" +"The file ${file//%/%%} was modified on %F at %R"
done发布于 2014-11-01 21:44:50
stat -c "The file "%n" was modified on ""%y" *Des* | awk 'BEGIN{OFS=" "}{for(i=1;i<=7;++i)printf("%s ",$i)}{print "at " substr($8,0,6)}'我在这里使用awk修改您的代码。我在这段代码中所做的,从字段1,7我打印它使用for循环,我需要修改字段8,所以我使用substr提取第一个5字符。
https://stackoverflow.com/questions/26693387
复制相似问题