Bash – stat: print time in “long-iso” format

bashlsstattime

For my script, I would like to have stat command to print time in a nice, human friendly, way: 2015-02-04 00:48:31. ls calls this format long-iso and it can be used like this:

$ ls -lA --time-style=long-iso .bashrc 
-rw------- 1 michael michael 5740 2015-02-04 00:48 .bashrc

However, there is no such switch for stat. The option %y for "human-readable time" looks like this:

$ stat -c'%A %h %U %G %s %y %n' .bashrc
-rw------- 1 michael michael 5740 2015-02-04 00:48:31.160827516 +0100 .bashrc

Is there any simple way to make stat print time in "long-iso" format?

I need to use stat rather than ls because I need to adjust which columns (attributes) get printed and in which order.

I am using stat form package coreutils verssion 8.13-3.5 on Debian.

Best Answer

The simplest way is to use the --printf option as suggested by @don_crissti:

stat --printf='%A %h %U %G %s %.16y %n\n' .bashrc

If, for whatever reason, you can't do that you can parse the output of `stat -c '%y':

$ stat -c'%A %h %U %G %s %y %n' .bashrc | awk '{$7=substr($7,1,8); $8=""}1;'
-rw-r--r-- 1 terdon terdon 9737 2015-02-01 18:12:18  .bashrc

Or you can use GNU date to convert it:

$ date -d "2015-02-01 18:12:18.665916181 +0200"
Sun Feb  1 19:52:18 EET 2015
$ date -d "2015-02-01 18:12:18.665916181 +020" +"%F %R:%S"
2015-02-01 19:52:18
Related Question