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

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?

Answer1:

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

Answer2:

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.

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:How can i format the output of stat expression in Linux Gnome Terminal?