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
file.c
View on GitHub ↗
static VALUE
rb_file_s_chown(int argc, VALUE *argv, VALUE _)
{
struct chown_args arg;
apply2args(2);
arg.owner = to_uid(*argv++);
arg.group = to_gid(*argv++);
return apply2files(chown_internal, argc, argv, &arg);
}
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
nilor-1owner or group id is ignored. -
The method follows symbolic links to the target entry.