Method: FileUtils.chmod

Defined in:
lib/fileutils.rb

.chmod(mode, list, noop: nil, verbose: nil) ⇒ Object

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:

    FileUtils.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:

    • targets may 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.

    • operator may 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:

    FileUtils.chmod('u=wrx,go=rx', 'src1.txt')
    FileUtils.chmod('u=wrx,go=rx', '/usr/bin/ruby')
    

Keyword arguments:

  • noop: true - does not change permissions; returns nil.

  • verbose: true - prints an equivalent command:

    FileUtils.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.


1804
1805
1806
1807
1808
1809
1810
1811
# File 'lib/fileutils.rb', line 1804

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