繁体   English   中英

如何在Linux Gnome Terminal中格式化stat表达式的输出?

[英]How can i format the output of stat expression in Linux Gnome Terminal?

我真的是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

我怎样才能做到这一点?

使用stat不能真正做到这一点(除非您拥有我不知道的stat的智能版本)。

date

您的date很可能足够聪明,并且可以处理-r开关。

date -r Desktop +"The file Desktop was modified on %F at %R"

由于存在问题,您将需要一个循环来处理所有与*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"

以及您的glob要求(以Bash格式):

shopt -s nullglob
for file in *Des*; do
    date -d "@$(stat -c '%Y' -- "$file")" +"The file ${file//%/%%} was modified on %F at %R"
done
 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提取了第1个5个字符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM