4

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.

2
  • 4
    Use stat with --printf=FORMAT option and format the date as per your taste, e.g.: stat --printf='%A %h %U %G %s %.16y %n\n' .bashrc or if you want it to show the seconds too: stat --printf='%A %h %U %G %s %.19y %n\n' .bashrc Commented Feb 14, 2015 at 10:18
  • 1
    @don_crissti - exactly what I needed. If you want to put it as answer, I would be happy to accept it. Commented Feb 14, 2015 at 11:16

1 Answer 1

5

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .