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

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