Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
FileUtils.chmod
# File tmp/rubies/ruby-3.3.0/lib/fileutils.rb, line 1803
def chmod(mode, list, noop: nil, verbose: nil)
list = fu_list(list)
fu_output_message sprintf('chmod %s %s', mode_to_s(mode), list.join(' ')) if verbose
return if noop
list.each do |path|
Entry_.new(path).chmod(fu_mode(mode, path))
end
end
Changes permissions on the entries at the paths given in list (a single path or an array of paths) to the permissions given by mode; returns list if it is an array, [list] otherwise:
-
Modifies each entry that is a regular file using
File.chmod. -
Modifies each entry that is a symbolic link using
File.lchmod.
Argument list or its elements should be interpretable as paths.
Argument mode may be either an integer or a string:
-
Integer
mode: represents the permission bits to be set:ExampleFileUtils.chmod(0755, 'src0.txt') FileUtils.chmod(0644, ['src0.txt', 'src0.dat'])
-
String
mode: represents the permissions to be set:The string is of the form
[targets][[operator][perms[,perms]], where:-
targetsmay be any combination of these letters:-
'u': permissions apply to the file’s owner. -
'g': permissions apply to users in the file’s group. -
'o': permissions apply to other users not in the file’s group. -
'a'(the default): permissions apply to all users.
-
-
operatormay be one of these letters:-
'+': adds permissions. -
'-': removes permissions. -
'=': sets (replaces) permissions.
-
-
perms(may be repeated, with separating commas) may be any combination of these letters:-
'r': Read. -
'w': Write. -
'x': Execute (search, for a directory). -
'X': Search (for a directories only; must be used with'+') -
's': Uid or gid. -
't': Sticky bit.
-
Examples:
ExampleFileUtils.chmod('u=wrx,go=rx', 'src1.txt') FileUtils.chmod('u=wrx,go=rx', '/usr/bin/ruby')
-
Keyword arguments:
-
noop: true- does not change permissions; returnsnil. -
verbose: true- prints an equivalent command:ExampleFileUtils.chmod(0755, 'src0.txt', noop: true, verbose: true) FileUtils.chmod(0644, ['src0.txt', 'src0.dat'], noop: true, verbose: true) FileUtils.chmod('u=wrx,go=rx', 'src1.txt', noop: true, verbose: true) FileUtils.chmod('u=wrx,go=rx', '/usr/bin/ruby', noop: true, verbose: true)
Output:
chmod 755 src0.txt chmod 644 src0.txt src0.dat chmod u=wrx,go=rx src1.txt chmod u=wrx,go=rx /usr/bin/ruby
Related: FileUtils.chmod_R.