Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

File.chown

(::int? owner, ::int? group, *::path file_name) -> ::Integer
File.chown(owner_int, group_int, *paths) → integer

Changes the owner and group of the entry at each of the given paths; returns the count of the given paths:

Example
# Super user; all privileges.
Process.uid                               => 0
Process.gid                               => 0
# Create a directory and a file.
dirpath = 'doc/foo'
Dir.mkdir(dirpath)
filepath = 't.tmp'
File.write(filepath, 'foo')
# Get their user and group ids.
dirstat = File::Stat.new(dirpath)
dirstat.uid                               => 0
dirstat.gid                               => 0
filestat = File::Stat.new(filepath)
filestat.uid                              => 0
filestat.gid                              => 0
# Change ownership of both.
File.chown(1000, 1000, filepath, dirpath) => 2
dirstat = File::Stat.new(dirpath)
dirstat.uid                               => 1000
dirstat.gid                               => 1000
filestat = File::Stat.new(filepath)
filestat.uid                              => 1000
filestat.gid                              => 1000
# Clean up.
Dir.rmdir(dirpath)
File.delete(filepath)

Notes:

  • On Windows, the owner and group are not changed.

  • Only a process with superuser privileges can change the owner of an entry.

  • The owner of an entry can change its group to any group to which the owner belongs.

  • A nil or -1 owner or group id is ignored.

  • The method follows symbolic links to the target entry.

See also #chown .chmod .ctime .chardev? also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0