0

So I used:

getent passwd $2 | awk -F: '{print "|Username: " $1 "\n|Password: " $2 "\n|Uid: " $3 "\n|Gid: " $4 "\n|Comment: " $5 "\n|Home: " $6 "\n|Shell: " $7 "\n"}'

to pipe user info in to a neat list.

I wanted to do the same with folders. I want all the information from the ' ls -l' command but instead of it coming out like a block of test I wanted it listed piece by piece.

Can I use an awk command together with ls and can I use the same method I did earlier?

Paulo Tomé
  • 3,754
  • 6
  • 26
  • 38
Fredrik
  • 11
  • 3
  • 1
    Not *reliably*, because the fields in the `ls -l` output are not unambiguously delimited like those in the `passwd` database. See for example [Why *not* parse `ls` (and what to do instead)?](https://unix.stackexchange.com/questions/128985/why-not-parse-ls-and-what-to-do-instead) – steeldriver Feb 18 '20 at 14:57
  • @Fredrik, add `groups | sed 's/ /, /g;s/^/Groups: /'` at the last line of your script. – Jetchisel Feb 19 '20 at 05:26

1 Answers1

5

Since you are on Linux you should instead look into the stat command with a proper format (-c, --format). Little known feature, the format accepts C-style length specifiers, so try for instance:

stat -c 'File: %-40n Size: %6s Flags: %8A' * 

You can put arbitrary strings in the format, and you can even put linefeeds in it (using the $'string' syntax):

stat -c $'--------------\nFile:     %n\nSize:     %s\nFlags:    %A' /boot/*
--------------
File:     /boot/abi-4.13.0-45-generic
Size:     1501528
Flags:    -rw-r--r--
--------------
File:     /boot/config-4.13.0-45-generic
Size:     213220
Flags:    -rw-r--r--
--------------
File:     /boot/config-4.15.0-72-generic
Size:     217468
Flags:    -rw-r--r--
--------------
File:     /boot/config-4.15.0-74-generic
Size:     217503
Flags:    -rw-r--r--
--------------
File:     /boot/config-4.15.0-76-generic
Size:     217503
Flags:    -rw-r--r--
xenoid
  • 8,648
  • 1
  • 24
  • 47