# Find out the name of a user, on a given file/dir/symlink?

**URL:** <https://www.ruby-forum.com/t/find-out-the-name-of-a-user-on-a-given-file-dir-symlink/169849>\
**Category:** Ruby\
**Created:** [June 9, 2009, 4:43pm UTC](https://www.ruby-forum.com/t/find-out-the-name-of-a-user-on-a-given-file-dir-symlink/169849 "2009-06-09T16:43:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [June 9, 2009, 4:43pm UTC](https://www.ruby-forum.com/t/find-out-the-name-of-a-user-on-a-given-file-dir-symlink/169849/1 "2009-06-09T16:43:21Z")

</div>

Right now I use this:

require ‘fileutils’  
require ‘etc’  
require ‘pp’

FileUtils.touch ‘test’

name = Etc.getpwuid(File.stat(‘test’).uid).name

puts name

This gives me the name of the file/dir/symlink in question, i.e  
who owns it.

However, I found this is a bit long. Anyone knows of a shorter  
way? Specifically I wonder if I have to use both Etc. and File.  
or if I can just one one of these two.

I didn’t find a quick way to just retrieve the name in class File  
though.

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [June 9, 2009, 5:47pm UTC](https://www.ruby-forum.com/t/find-out-the-name-of-a-user-on-a-given-file-dir-symlink/169849/2 "2009-06-09T17:47:59Z")

</div>

2009/6/9 Marc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> puts name
> 
> This gives me the name of the file/dir/symlink in question, i.e  
> who owns it.
> 
> However, I found this is a bit long. Anyone knows of a shorter  
> way? Specifically I wonder if I have to use both Etc. and File.  
> or if I can just one one of these two.

As far as I can see you need both because File.stat only returns the  
numeric ID.

irb(main):008:0\> st = File.stat “.”  
=\> #\<File::Stat dev=0x50a951b9, ino=6755399441101867, mode=040755,  
nlink=9, uid=14366, gid=10513, rdev=0x50a951b9, size=0, blksize=65536,  
blocks=0, atime=2009-06-09 17:44:07 +0100, mtime=2009-06-09 17:35:03  
+0100, ctime=2009-06-09 17:35:03 +0100\>  
irb(main):009:0\> st.owned?  
=\> true  
irb(main):010:0\> st.uid  
=\> 14366  
irb(main):011:0\>

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [June 9, 2009, 5:48pm UTC](https://www.ruby-forum.com/t/find-out-the-name-of-a-user-on-a-given-file-dir-symlink/169849/3 "2009-06-09T17:48:34Z")

</div>

> require ‘pp’
> 
> However, I found this is a bit long. Anyone knows of a shorter  
> way? Specifically I wonder if I have to use both Etc. and File.  
> or if I can just one one of these two.
> 
> I didn’t find a quick way to just retrieve the name in class File  
> though.

That’s about as good as it gets. You can always re-open the File class  
if this is something you need on a regular basis:

class File  
def self.owner(file)  
Etc.getpwuid(stat(file).uid).name  
end  
end

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [June 9, 2009, 5:57pm UTC](https://www.ruby-forum.com/t/find-out-the-name-of-a-user-on-a-given-file-dir-symlink/169849/4 "2009-06-09T17:57:21Z")

</div>

> That’s about as good as it gets. You can always re-open the File class  
> if this is something you need on a regular basis:

Thanks to both, I guess I will extend File for the project.

Cheers.

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [June 10, 2009, 1:45pm UTC](https://www.ruby-forum.com/t/find-out-the-name-of-a-user-on-a-given-file-dir-symlink/169849/5 "2009-06-10T13:45:41Z")

</div>

2009/6/9 Marc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > That’s about as good as it gets. You can always re-open the File class  
> > if this is something you need on a regular basis:
> 
> Thanks to both, I guess I will extend File for the project.

I do not know how fast Etc.getpwuid is but I assume it might be slow  
because the information has to be retrieved via system calls and  
potentially grabbed from persistent storage. If you need this  
frequently then you might want to cache entries, e.g.

USER\_NAME = Hash.new {|h,uid| h[uid] = uid ? Etc.getpwuid(uid).name :  
“”}

files.each do |f|  
puts f, USER\_NAME[File.stat(f).uid]  
end

Kind regards

robert
