Skip to content
class method

FileUtils.chmod

(::FileUtils::mode mode, ::FileUtils::pathlist list, ?noop: ::boolish, ?verbose: ::boolish) -> void
chmod(mode, list, noop: nil, verbose: nil)

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:

    Example
    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:

    Example
    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:

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

See also #chmod .chdir .chmod_R .cd also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0