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

不羁的心 提交于 2020-01-04 17:29:07

问题


I am really newbie in Linux(Fedora-20) and I am trying to learn basics I have the following command

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?


回答1:


You can't really do that with stat (unless you have a smart version of stat I'm not aware of).

With date

Very likely, your date is smart enough and handles the -r switch.

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):

shopt -s nullglob
for file in *Des*; do
    date -r "$file" +"The file ${file//%/%%} was modified on %F at %R"
done

With find

Very likely your find has a rich -printf option:

find . -maxdepth 1 -name '*Des*' -printf 'The file %f was modified on %TY-%Tm-%Td at %TH:%TM\n'

I want to use 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). 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):

shopt -s nullglob
for file in *Des*; do
    date -d "@$(stat -c '%Y' -- "$file")" +"The file ${file//%/%%} was modified on %F at %R"
done



回答2:


 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. 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.



来源:https://stackoverflow.com/questions/26693387/how-can-i-format-the-output-of-stat-expression-in-linux-gnome-terminal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!