0

I'd list as much as possible to replicate ls -l with stat so that I can see the octal numbers instead.

I have got quite close with stat -c '%a------- %h %U %G %5s %.16y %n' *

me@PC:/etc/apt$ ls -l
total 76
drwxr-xr-x 2 root root  4096 Sep  2 15:50 apt.conf.d
drwxr-xr-x 2 root root  4096 Apr  9  2020 auth.conf.d
drwxr-xr-x 2 root root  4096 Jul 28 03:26 keyrings
drwxr-xr-x 2 root root  4096 Jul 12  2021 preferences.d
-rw-rw-r-- 1 root root  2796 Aug 23 11:02 sources.list
drwxr-xr-x 2 root root  4096 Sep  2 15:50 sources.list.d
-rw-rw-r-- 1 root root  3071 Aug 23 11:02 sources.list.distUpgrade
-rw-rw-r-- 1 root root  3071 Jun 27 13:04 sources.list.save
-rw-r--r-- 1 root root 18951 Mar 27 17:15 trusted.gpg
-rw-r--r-- 1 root root 17320 Feb  7  2022 trusted.gpg~
drwxr-xr-x 2 root root  4096 Sep  2 15:50 trusted.gpg.d

me@PC:/etc/apt$ stat -c '%a------- %h %U %G %5s %.16y %n' * 755------- 2 root root 4096 2022-09-02 15:50 apt.conf.d 755------- 2 root root 4096 2020-04-09 06:21 auth.conf.d 755------- 2 root root 4096 2022-07-28 03:26 keyrings 755------- 2 root root 4096 2021-07-12 06:26 preferences.d 664------- 1 root root 2796 2022-08-23 11:02 sources.list 755------- 2 root root 4096 2022-09-02 15:50 sources.list.d 664------- 1 root root 3071 2022-08-23 11:02 sources.list.distUpgrade 664------- 1 root root 3071 2022-06-27 13:04 sources.list.save 644------- 1 root root 18951 2022-03-27 17:15 trusted.gpg 644------- 1 root root 17320 2022-02-07 22:44 trusted.gpg~ 755------- 2 root root 4096 2022-09-02 15:50 trusted.gpg.d

All the data is there but the date format is slightly off.

Can it be piped through xargs and printf or something to reformat the date or is the closest I can get?

  • Since you mention piping through printf, have you tried output formatting via the --printf option? – Sotto Voce Sep 06 '22 at 22:17
  • 1
    the date format in LS is actually mixed, as it only shows either year or time of day, depending on the age of the file. any such script would have to switch formats to mimic LS's behavior – toppk Sep 06 '22 at 22:26
  • I like the idea very much, but let me correct your state: All the data is there AND the date format is MUCH BETTER! :) (I use ls always with long-iso timeformat) Good job btw, after octal code why do you add 6 dashes? – gabor.zed Sep 07 '22 at 13:16
  • That seems to be the GNU implementation of stat you're using. Note that there are many different implementations of a stat command completely incompatible between each other. – Stéphane Chazelas Sep 07 '22 at 13:53
  • 1
    The ls timestamp format also varies with the locale and system, and also whether the file was last modified in the last 6 months or earlier or in the future. – Stéphane Chazelas Sep 07 '22 at 13:55
  • With GNU stat you want %A for the type and permissions in symbolic form. – Stéphane Chazelas Sep 07 '22 at 13:56
  • And ls mishandles funny characters in filenames. See https://mywiki.wooledge.org/ParsingLs – waltinator Sep 08 '22 at 05:00
  • @gabor.zed I add the extra dashes so that the columns line up with the ls output and the ls output has dashes in it so it looks a bit similar – opticyclic Sep 08 '22 at 13:15
  • Check this: https://unix.stackexchange.com/questions/76521/how-can-i-display-octal-notation-of-permissions-with-ls-and-can-octal-represen – gabor.zed Sep 08 '22 at 20:58

1 Answers1

1

Here is a little script, to start with:

IFS=$'\n'
for i in $(stat --printf="%a\t%h\t%U\t%G\t%s\t%Y\t%n\n" *); do
  IFS=$'\t' read -r -a FP <<<$i; 
  echo ${FP[0]} ${FP[1]} ${FP[2]} ${FP[3]} ${FP[4]} $(date -d@${FP[5]} "+%Y.%m.%d. %H:%M") ${FP[6]} 
done

This way you can handle every file property. For example if the username or group is too long, ls shows the uid/gid instead, and so on.

gabor.zed
  • 157
  • 6