Common Linux commands (7): chmod command (give read and write permissions to files/chmod 777)

1. Introduction to commands

chmod is used to change the access permissions of files or directories.

chmod can only be used by the file owner and the super user root.

There are three scopes of permissions for Linux files and directories: owner (u, user), group (g, group) and other (o, other). All users are represented by a (all). The permissions of each range are represented by three bits, which from left to right represent r (read, read), w (write, write) and x (execute, executable). Permissions can be represented by characters or octal numbers, r corresponds to the number 4, w corresponds to the number 2, and x corresponds to the number 1. If the corresponding permission bit does not have permission, it will be displayed as a horizontal bar - when using the ls -l command to view it. As shown below:
Insert image description here

Note:
(1) When using chmod to change the permissions of a symbolic link, what is actually changed is the permissions of the target file; chmod cannot change the permissions of its symbolic link. This is not a bug because the permissions of the symbolic link have never been used.
(2) When chmod is recursive, it will ignore symbolic links when encountering them.

1.1. Explain the meaning of linux drwxr-xr-x

Insert image description here
As shown above, we see that the file has drwxr-xr-x permissions:

第一位表示文件类型。d是目录文件,l是链接文件,-是普通文件,p是管道

第2-4位表示这个文件的属主拥有的权限,r是读,w是写,x是执行。

第5-7位表示和这个文件属主所在同一个组的用户所具有的权限。

第8-10位表示其他用户所具有的权限。

2. chmod syntax

chmod [-cfvR] [--help] [--version] mode file...

Parameter Description

mode: permission setting string, the format is as follows:

[ugoa...][[+-=][rwxX]...][,...]

in:

  • u represents the owner of the file, g represents the person who belongs to the same group as the owner of the file, o represents other people, and a represents all three.
  • + means adding permissions, - means canceling permissions, = means setting only permissions.
  • r means readable, w means writable, x means executable, and X means only when the file is a subdirectory or the file has been set to be executable.

Other parameter descriptions:

  • -c: If the file permissions have indeed been changed, the change action will be displayed.
  • -f : Do not display an error message if the file permissions cannot be changed
  • -v: Display details of permission changes
  • -R: Make the same permission changes to all files and subdirectories in the current directory (that is, change them one by one in a recursive manner)
  • –help: show help information
  • –version : display version

3. Examples

(1) Make all files and subdirectories in the current directory readable, writable, and executable by anyone:

chmod -R 777 *
或
chmod -R a=rwx *

(2) Make the file file1.txt readable by everyone:

chmod ugo+r file1.txt
或
chmod a+r file1.txt
或
chmod +r file1.txt

(3) Subtract the execution permissions of all users on the file test.sh.

chmod -x test.sh
# 或
chmod a-x test.sh

(4) Set new permissions for the file test.sh to rwxr–r– using symbolic and numerical methods respectively.

# 符号方式
chmod a=rwx,g=r,o=r test.sh

# 数字方式
chmod 744 test.sh

Reference article:
https://dablelv.blog.csdn.net/article/details/102827264

Guess you like

Origin blog.csdn.net/weixin_49114503/article/details/132994396