• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All
  • About
Linux-Ubuntu | chmod -- change file modes
  1. Notes
  2. Examples
  3. Command Help (man chmod)

  1. Notes
    chmod mode file ...

    The chmod command can be used to modify the file mode of files and directories.

    The chmod command can also be used to modify the Access Control Lists (ACLs) associated with files and directories: chmod -- change file ACLs (Access Control Lists).

    Files (and directories) have the ownership set for a user, group, or others. Each of them can define three regular types of permissions: read (r), write (w), and execute (x). The dash (-) character is used when a permission is not set. A file (or directory) permissions are set using 9 bits (rwxrwxrwx) that define the read, write, and execute permissions for the user, group, and others. The first 3 bits defines the read, write, and execute permissions for the user, the next three bits for the group, and the last three bits for others.

    By default permissions of a new created file (or directory) is set using the value of umask (default 002). To set the permission of the new created file (or directory), the value of umask 002 is combined with the default permissions of a file which is 666 (or directory 777). So by default a new created file has the permissions rw-rw-r-- (664) and new created directory has the permissions rwxrwxr-x (775).
  2. Examples
    • Set the permission "read", "write", and "execute" to "user", "group", and "others" on the file "file1".

      Use any of the following commands:
      $ chmod 777 file1
      
      $ chmod ugo+rwx file1
      
      $ chmod ugo=rwx file1
      
      $ chmod a=rwx file1

    • Set the permission "read", "write", and "execute" to "user" and set the permission "read" and "execute" to "group" and "others" on the file "file1".

      Use any of the following commands:
      $ chmod 755 file1
      
      $ chmod u=rwx,go=rx file1
      
      $ chmod u=rwx,go=u-w file1

    • Add the permission "read" and "write" to "user" and "group" on the file "file1".
      If user or group have already the execute permission, they will keep this permission.

      $ chmod ug+rw file1

    • Deny the permission "read" and "write" to "user" and "group" on the file "file1".
      If user or group have already the execute permission, they will keep this permission.

      $ chmod ug-rw file1

    • Deny all permission to "group" and "others" on the file "file1".

      Use any of the following commands:
      $ chmod go-rwx file1
      
      $ chmod go= file1

    • Set the permission "read", "write", and "search/execute" to "user", "group", and "others" on the directory "folder1" and all its files and sub-directories.

      Use any of the following commands:
      $ chmod -R 777 folder1/
      
      $ chmod -R ugo+rwx folder1/
      
      $ chmod -R ugo=rwx folder1/
      
      $ chmod -R a=rwx folder1/
  3. Command Help (man chmod)
    • The following options can be used:

      -R
      |If chmod is applied to a directory, it will change the mode of this directory and all its files and sub-directories.
      |By default, the linked files of symbolic links will not be changed.

      -L
      |If the -R option is specified, the linked files of symbolic links will be changed.

      -v
      |Cause chmod to be verbose, showing filenames if the mode is modified.
      |If the -v flag is specified twice, the old and new modes of the file will also be printed, in both octal and symbolic notation.

    • The symbolic mode can be described by one of the following grammar.

      Short syntax:
      [ugoa]*([-+=]([rwx]*|[ugo]))+

      Detailed syntax:
      mode       ::= clause [, clause ...]
      clause     ::= [who ...] [action ...] action
      action     ::= operation [permission ...]
      who        ::= u | g | o | a
      operation  ::= + | - | =
      permission ::= r | w | x | u | g | o

    • The who symbols specify:
      u: user
      g: group
      o: others
      a: all (equivalent to "ugo")

    • The permission symbols specify:
      r: read permission.
      w: write permission.
      x: execute permission (files) | search permission (directories).

    • The permission symbols can be represented by numeric values as follow:
      r (read) = 4
      w (write) = 2
      x (execute) = 1

      r+w (read and write) = 6
      r+x (read and execute) = 5
      w+x (write and execute) = 3

      r+w+x (read and write and execute) = 7

    • The operation symbols specify:
      +
      |If no value is supplied for permission, the "+" operation has no effect.
      |If no value is supplied for who, each listed permission is set for the owner.
      |Otherwise, the mode of the specified who and permission values are set.

      -
      |If no value is supplied for permission, the "-" operation has no effect.
      |If no value is supplied for who, each listed permission is cleared for the owner, group and others.
      |Otherwise, the mode of the specified who and permission values are cleared.

      =
      |If no value is supplied for permission, all permissions are cleared for the listed values of who.
      |If no value is supplied for who, each listed permission is set and non listed permission is cleared, for the owner, group and others.
      |If no value is supplied for both permission and who, all permissions are cleared for the owner, group and others.
      |Otherwise, each listed permission is set and non listed permission is cleared, for the listed values of who.
© 2025  mtitek