Used to change the permissions of files or directories
chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...
mode: Octal mode or symbolic combination.
file: One or more files to change permissions for.
-c, --changes: Like verbose but report only when a change is made.
--no-preserve-root: Do not treat '/' specially (the default).
--preserve-root: Fail to operate recursively on '/'.
-f, --silent, --quiet: Suppress most error messages.
-v, --verbose: Output a diagnostic for every file processed.
--reference=RFILE: Use RFILE's mode instead of MODE values.
-R, --recursive: Change files and directories recursively.
--help: Display help information and exit.
--version: Display version information and exit.
Returns success unless invalid options or arguments are provided.
Referring to the
DESCRIPTIONsection ofman chmod:
urepresents the current user (owner).grepresents users in the same group as the owner.orepresents other users.arepresents all users.rrepresents read permission (octal 4).wrepresents write permission (octal 2).xrepresents execute permission (octal 1).Xrepresents execute permission only if the file is a directory or already has execute permission for some user.srepresents set user or group ID on execution (SUID/SGID).trepresents the restricted deletion flag or sticky bit.+adds specified permissions.-removes specified permissions.=sets specified permissions and removes others.
Explanation of Linux file permissions:
# View current directory (including hidden files) in long format.
ls -la
-rw-r--r-- 1 user staff 651 Oct 12 12:53 .gitmodules
# The 1st character: 'd' for directory, '-' for regular file.
# For more details, see 'info coreutils 'ls invocation'' section for '-l' option.
# Characters 2-4: Owner permissions.
# Characters 5-7: Group permissions.
# Characters 8-10: Other users' permissions.
# Add write permission for the group.
chmod g+w ./test.log
# Remove all permissions for other users.
chmod o= ./test.log
# Remove write permission for all users.
chmod a-w ./test.log
# Owner has all permissions, group has read/write, others have read only.
chmod u=rwx,g=rw,o=r ./test.log
# Equivalent octal representation:
chmod 764 ./test.log
# Set directory and its contents to read/write for all users.
# Note: When using '-R', ensure the current user retains execute and read permissions!
chmod -R a=rw ./testdir/
# Set file permissions based on another file.
chmod --reference=./1.log ./test.log
This command is part of the GNU coreutils package. For related help, see man chmod or info coreutils 'chmod invocation'.
Permissions of symbolic links cannot be changed; changes applied to a symbolic link affect the target file.
When using the -R option, ensure the current user retains execute and read permissions, otherwise an error will occur!