Using `ls` to show real name of file owners

0

1

Unix users can have their real names listed in /etc/passwd/. The finger command can display the real names. Can ls -l show these real names of file owners next to the files?

If ls cannot do this directly, then what's the best way to pipe the output of ls -l through finger to achieve this result? Sounds like a job for awk - use ls -l | finger as a lookup table to overwrite the third column of ls -l.

(Related question in case the real names are not in finger but in a lookup table: Lookup-table text replacement in bash)

root

Posted 2018-10-19T10:50:16.310

Reputation: 272

1

Why are you asking two separate questions? It looks like you're trying to solve one problem and you thought of two approaches. Compare XY problem. Would it be OK if you asked about X once? and only mentioned you think Y1 or Y2 would help. Maybe the best solution is some Y3 none of us knows yet. So why the two questions?

– Kamil Maciorowski – 2018-10-19T10:59:47.163

Two questions because they are for two different settings. In one setting, the names are available in finger (and maybe ls can access that info). In the other question, the real names are in a file but not in finger. – root – 2018-10-19T11:12:47.173

1Why not parse ls? You shouldn't try that: ls -l | awk 'BEGIN{lc=0; while((getline line<"/etc/passwd") >0){split(line,p,":"); map[p[1]]=p[5]; lc++}} {$3=map[$3]}1' I assume that the third column of your ls -l command contains the user. – Cyrus – 2018-10-19T12:58:51.037

1

As mentioned above, don't parse the output of ls (it's for people, not machines)... additionally, finger isn't really designed for this at all... prefer something like getent passwd ${USER}. To make things worse though, I don't think that the 4th field of passwd is standardized... see GECOS

– Attie – 2018-10-19T13:15:01.553

Answers

0

You can parse ls as @Cyrus mentioned in the comments. However, I suggest a simple script to do that, which you can make as an executable:

while read -r line
do
    user=`echo "$line" | awk '{print $3}'`
    name=`getent passwd "$user" | cut -d ':' -f 5 | cut -d ',' -f 1`
    echo $line | sed "s/$user/$name/"
done < <(ls -lh | tail -n +2)

The user variable gets the username of the owner (from the 3rd column of the ls output), name gets that username's real name, and then in the output of ls we just replace the username with the real name using sed.

I haven't used finger in my example, as parsing the /etc/passwd file is less error-prone and you also do not need an external tool. (finger isn't installed on Debian/Ubuntu by default)

Example output:

-rwxr-xr-x 1 Pavlin Nikolov fanatique 41 Aug 29 15:13 cpu.sh
-rwxr-xr-x 1 Pavlin Nikolov fanatique 193 Aug 28 11:36 httpingwrap.sh
-rwxr-xr-x 1 Pavlin Nikolov fanatique 3.8K Aug 14 15:36 imgconv.py
-rwxr-xr-x 1 Pavlin Nikolov fanatique 504 Oct 19 16:04 lsp
-rwxr-xr-x 1 Pavlin Nikolov fanatique 77 Aug 28 14:24 mg
-rwxrwxrwx 1 Pavlin Nikolov fanatique 386 Sep 14 15:02 mon-install.sh
-rwxr-xr-x 1 Pavlin Nikolov fanatique 589 Sep 11 11:55 ping-wrapper.py

Note that it is not the best-looking output, as it is not aligned perfectly, but it should still work.

Fanatique

Posted 2018-10-19T10:50:16.310

Reputation: 3 475

Why not parse ls (and what do to instead)? ... ls -lh | tail -n +2 is just the beginning... You should at least be using --numeric-uid-gid if you're going to try :-) – Attie – 2018-10-19T13:15:58.797