LoginSignup
5

More than 5 years have passed since last update.

Rubyでファイルの所有ユーザ・グループを調べる

Last updated at Posted at 2015-06-01

ミドルウェアの設定ファイルなどを書くときにディレクトリの権限をいじる必要が時たまあると思う。1個ならlsたたけばすぐわかるけどいっきにいろいろなディレクトリを調べたいときにはコードで書く必要がありそうなので調べた。

Fileコマンドでは取れなかったのでEtcや、File::Statを利用したりする。

require 'etc'

path = '/'
user = Etc.getpwuid(File.stat(path).uid).name
group = Etc.getgrgid(File.stat(path).gid).name
puts "#{user}:#{group} #{path}" #=> root:root /

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5