1

With the command stat * --format='%A %h %U %G %s %z %n' I get:

-rwxrwxrwx 1 myuser mygroup 131072 2021-11-12 14:52:23.495595927 +0100 myfile

Is it possible to have a stat output like the following? Note that the date is in another format.

-rwxrwxrwx 1 myuser mygroup 131072 Nov 12 14:52:23 2021 myfile

This is the like the output of ls -l, but in another post they told me not to parse the output of ls -l myfile, that's why I asked if it is possibile to do it with stat.

16
  • 3
    That is the format you would get with ls -l myfile . So you can just use that command.
    – Garo
    Jan 5, 2022 at 14:31
  • @Garo In another post they told me not to parse the output of ls -l myfile: unix.stackexchange.com/questions/684171/…, that's why I asked if it is possibile to do it with stat.
    – CarLaTeX
    Jan 5, 2022 at 14:37
  • Get the timestamp as seconds from epoch, and use date to convert it to another format.
    – Panki
    Jan 5, 2022 at 14:45
  • 3
    changing ls to stat won't help you with the main issue, which is filenames that can contain any character, even newline. stat --format="%n" happily prints the embedded newline and messes up your output. You need something like stat --printf='%A %h %U %G %s %z %n\0' to get an unambiguous output with a NUL separator. Or use %N for quoted output, if you can deal with that. Then there's also the question of why you'd want the ls date format anyway, since the ISO 8601 one, or just plain seconds after the epoch are far easier to parse.
    – ilkkachu
    Jan 5, 2022 at 14:55
  • 2
    @CarLaTeX, well, ok, but you still need to deal with the issues in mywiki.wooledge.org/ParsingLs. Or if you decide they aren't issues, then you might as well use ls. It's not about what the tool is called, but about having parsable, unambiguous and issue-free output.
    – ilkkachu
    Jan 5, 2022 at 15:04

1 Answer 1

2

I'm afraid you need to pipe the epoch second output into some "converter". Like

stat file --format='%A %h %U %G %s %Z %n' | { read A h U G s Z n; printf "%s %s %s %s %s %(%b %d %T %Y)T %s\n" $A $h $U $G $s $Z $n; }
-rw-rw-r-- 1 user user 21 Dez 30 14:17:03 2021 file

making use of bash's %()T format specifier to printf.

2
  • This works but not only for one file, not all the files of the directory.
    – CarLaTeX
    Jan 5, 2022 at 16:56
  • Use a while loop, then.
    – RudiC
    Jan 6, 2022 at 10:42

You must log in to answer this question.

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