Stack Exchange
Get socket and pipe name in CentOS
Basically, I want to get socket and pipe owners starting from a PID located in the /proc/PID/fd/ directory using the "stat" command:
lrwx------ 1 root root 64 dic 23 17:52 0 -> socket:[9790]
l-wx------ 1 root root 64 dic 23 17:52 1 -> /var/log/messages
l-wx------ 1 root root 64 dic 23 17:52 2 -> /var/log/secure
lr-x------ 1 root root 64 dic 23 17:52 3 -> /proc/kmsg
l-wx------ 1 root root 64 dic 23 17:52 4 -> /var/log/maillog
l-wx------ 1 root root 64 dic 23 17:52 5 -> /var/log/cron

How could I use "stat" to get each socket owner when I just have the socket name (socket:[9790]) instead of its symbolic reference (0,1,2...)?
Thanks for your answer!

StackOverflow's scope is limited to writing code.  [unix.se] or SuperUser are more appropriate for questions about using standard UNIX tools.

For this lsof is much better. 
You can use lsof -i -a -p $PID.
There is a similar question on the unix hub on the StackExchange.
https://unix.stackexchange.com/questions/235979/how-do-i-find-out-more-about-socket-files-in-proc-fd
I think that should solve your problem.

I do not think that you can do this with one simple stat instruction. Instead you could try to utilize find in order to search for symbolic links an then print them formatted. You can then, e.g., pipe the result to grep and cut to filter for the information you need:
find /proc/PID/fd/ -type l -printf '%u %l\n' 2>/dev/null|grep "socket:[9790]"|cut -d' ' -f1

You can use find's options (-mindepth, -maxdepth) in order to determine how deep it shall descend into subfolders for its search. If you want the owner's UID instead of the username then use %U instead of %u in -printf.
