[英]How can i format the output of stat expression in Linux Gnome Terminal?
I am really newbie in Linux(Fedora-20) and I am trying to learn basics I have the following command 我真的是Linux(Fedora-20)的新手,我想学习基础知识,我有以下命令
echo "`stat -c "The file "%n" was modified on ""%y" *Des*`"
This command returns me this output 此命令返回此输出
The file Desktop was modified on 2014-11-01 18:23:29.410148517 +0000
I want to format it as this: 我想这样格式化:
The file Desktop was modified on 2014-11-01 at 18:23
How can I do this? 我怎样才能做到这一点?
You can't really do that with stat
(unless you have a smart version of stat
I'm not aware of). 使用
stat
不能真正做到这一点(除非您拥有我不知道的stat
的智能版本)。
date
date
Very likely, your date
is smart enough and handles the -r
switch. 您的
date
很可能足够聪明,并且可以处理-r
开关。
date -r Desktop +"The file Desktop was modified on %F at %R"
Because of your glob, you'll need a loop to handle all files that match *Des*
(in Bash): 由于存在问题,您将需要一个循环来处理所有与
*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
Very likely your find
has a rich -printf
option: 您的
find
很有可能具有丰富的-printf
选项:
find . -maxdepth 1 -name '*Des*' -printf 'The file %f was modified on %TY-%Tm-%Td at %TH:%TM\n'
stat
stat
(because your date
doesn't handle the -r
switch, you don't want to use find
or just because you like using as most tools as possible to impress your little sister). (因为您的
date
无法处理-r
开关,因此您不想使用find
或只是因为您希望使用尽可能多的工具来打动妹妹)。 Well, in that case, the safest thing to do is: 好吧,在这种情况下,最安全的做法是:
date -d "@$(stat -c '%Y' Desktop)" +"The file Desktop was modified on %F at %R"
and with your glob requirement (in Bash): 以及您的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)}'
I have use here awk
modify your code. 我在这里用
awk
修改您的代码。 what i have done in this code, from field 1,7 i printed it using for loop, i need to modify field 8, so i used substr
to extract 1st 5 character. 我在这段代码中所做的事情,是从字段1,7中使用for循环将其打印出来的,我需要修改字段8,因此我用
substr
提取了第1个5个字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.