TrumanWong

chmod

Used to change the permissions of a file or directory

Summary

chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...

The main purpose

  • Change the permissions of the target file or directory through a combination of symbols.
  • Change the permissions of the target file or directory using octal numbers.
  • Change the permissions of the target file or directory by referring to the file's permissions.

Parameters

mode: octal number or symbol combination.

file: Specify one or more files whose permissions are to be changed.

Options

-c, --changes: Output operation information when the permissions of the file are changed.
--no-preserve-root: Do not specialize '/', default option.
--preserve-root: Cannot operate recursively in the root directory.
-f, --silent, --quiet: Suppress the output of most error messages.
-v, --verbose: Regardless of whether the file permissions have been changed, operation information will always be output.
--reference=RFILE: Use the permissions of the reference file or reference directory RFILE to set the permissions of the target file or directory.
-R, --recursive: Recursively change permissions on the directory and files under the directory.
--help: Display help information and exit.
--version: Display version information and exit.

return value

The return status is success unless illegal options or illegal parameters are given.

example

Refer to the DESCRIPTION paragraph of the man chmod document to learn:

  • The u symbol represents the current user.
  • The g symbol represents users who are in the same group as the current user, hereafter referred to as group users.
  • The o symbol represents other users.
  • The a symbol represents all users.
  • The r symbol represents read permission and the octal number 4.
  • The w symbol represents write permission and the octal number 2.
  • The x symbol represents execute permission and the octal number 1.
  • The X symbol means that if the target file is an executable file or directory, executable permissions can be set for it.
  • The s symbol represents setting permissions suid and sgid, using the permission combination u+s to set the user ID bit of the file, and g+s to set the group user ID bit.
  • The t symbol means that only the owner of the directory or file can delete files in the directory.
  • The + symbol represents adding the corresponding permissions of the target user.
  • The - symbol represents deleting the corresponding permissions of the target user.
  • The = symbol represents adding the corresponding permissions of the target user and deleting the permissions not mentioned.
Description of user permissions for linux files:

# View the long format of the current directory (including hidden files).
ls -la
   -rw-r--r-- 1 user staff 651 Oct 12 12:53 .gitmodules

# If the first bit is d, it represents a directory, and if it is -, it represents an ordinary file.
# For more details, see the '-l' option section of info coreutils 'ls invocation' (info document of ls command).
# Digits 2 to 4 represent the permissions of the current user.
# The 5th to 7th digits represent the permissions of the group user.
# Bits 8 to 10 represent the permissions of other users.
# Add write permissions for group users.
chmod g+w ./test.log
# Remove all permissions of other users.
chmod o= ./test.log
# So that all users do not have write permission.
chmod a-w ./test.log
# The current user has all permissions, group users have read and write permissions, and other users only have read permissions.
chmod u=rwx, g=rw, o=r ./test.log
# Equivalent octal number representation:
chmod 764 ./test.log
# Set the directory and files in the directory to have read and write permissions for all users.
# Note that when using the '-R' option, you must retain the execution and read permissions of the current user, otherwise an error will be reported!
chmod -R a=rw ./testdir/
# Set file permissions based on the permissions of other files.
chmod --reference=./1.log ./test.log

Notice

  1. This command is a command in the GNU coreutils package. For related help information, please see man chmod or info coreutils 'chmod invocation'.

  2. The permissions of a symbolic link cannot be changed. If the user modifies the permissions of a symbolic link, the changes will be applied to the original file being connected.

  3. When using the -R option, be sure to retain the execution and read permissions of the current user, otherwise an error will be reported!