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.
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.163Two questions because they are for two different settings. In one setting, the names are available in
finger
(and maybels
can access that info). In the other question, the real names are in a file but not infinger
. – root – 2018-10-19T11:12:47.1731Why 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 yourls -l
command contains the user. – Cyrus – 2018-10-19T12:58:51.0371
As mentioned above, don't parse the output of
– Attie – 2018-10-19T13:15:01.553ls
(it's for people, not machines)... additionally,finger
isn't really designed for this at all... prefer something likegetent passwd ${USER}
. To make things worse though, I don't think that the 4th field of passwd is standardized... see GECOS