如何在Linux Gnome终端中格式化stat表达式的输出?
我真的是Linux(Fedora-20)的新手,我正在努力学习基础知识 我有以下命令如何在Linux Gnome终端中格式化stat表达式的输出?,linux,bash,terminal,fedora,stat,Linux,Bash,Terminal,Fedora,Stat,我真的是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 我该怎
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
我该怎么做呢?
stat-c“文件“%n”是在“%y”*Des*|awk'BEGIN{OFS=”“}{for(I=1;I你不能用stat
真正做到这一点的(除非你有stat
的智能版本,我不知道)
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)}'
带有日期
很可能,您的日期
足够智能,可以处理-r
开关
date -r Desktop +"The file Desktop was modified on %F at %R"
由于您的glob,您需要一个循环来处理与*Des*
匹配的所有文件(在Bash中):
使用查找
很可能您的find
有一个丰富的-printf
选项:
find . -maxdepth 1 -name '*Des*' -printf 'The file %f was modified on %TY-%Tm-%Td at %TH:%TM\n'
我想使用stat
(因为你的日期
无法处理-r
开关,你不想使用查找
,或者只是因为你喜欢使用尽可能多的工具来打动你的小妹妹)。那么,在这种情况下,最安全的做法是:
date -d "@$(stat -c '%Y' Desktop)" +"The file Desktop was modified on %F at %R"
根据您的glob需求(在Bash中):
它适用于文件有空间的情况,因此不需要echo。不,它不适用于包含空格的文件名。请自己尝试:触摸“桌面”
,然后尽情享受。
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