1. Detailed explanation of file attributes
![]() |
drwxr-xr-x
Properties are mainly divided into 4 parts:
first part
The first letter, indicating the file type
- d means directory
- — for ordinary files
- b represents input and output devices (random access devices)
- l means link file
- c represents the serial port device in the device file, such as keyboard, mouse (one-time read device)
part 2
From the 2nd character to the 4th character, indicating the file owner permission
part 3
From the 5th character to the 7th character, it means the permission of the file group
part 4
From the 8th character to the 10th character, indicating the authority of other users
Where r means read permission, w means write permission, x means execute permission, - means no permission
Second, change the file permission command chmod
There are two ways to set file attributes, one is numbers and the other is symbols.
For example, drwxr-xr-x
the permissions of the current test file are:
- owner - readable writeable executable
- User group - readable and executable, not writable
- Other users - readable and executable, not writable
1. Represented by numbers
chmod [-R] xyz 文件或目录
Options and parameters:
- -R performs recursive (recursive) continuous changes, that is, all files in the subdirectory will be changed
- Where xyz represents the permission number
Each permission symbol can be represented numerically:
- r : 4
- w : 2
- x : 1
The three permissions (r/w/x) of each identity (owner/group/others) need to accumulate scores. For example, when the permissions are -rwxr-xr-x
scores:
- owner 4+2+1 = 7
- group 4+0+1 = 5
- others 4+0+1 = 5
When we change the permissions of the test.sh file, it is represented by numbers:
chmod 755 test
If you want to change the permissions of the files in the directory together, execute
chmod -R 755 test
2. Represented by symbols
chmod [ugoa] [[+ / - / =] [rwx]] [文件名]
The + means to add permissions, - means to subtract permissions, = means to set permissions
The permissions of the test.sh above are represented by symbols -rwxr-xr-x
:
chmod u=rwx g=rx o=rx
3. The super user changes the file owner and associated group chown
注意
:chown requires superuser root
privileges to use, only superusers and file owners belonging to the group can change the file association group. Non-superusers may need to use the chgrp command if they need to set an association group.
-
Set the owner of test to user1:
chown user1 test
-
Change the owner of test to root, and the group it belongs to is root:
chown root:root test
-
Set the owner of all files and subdirectories in the current directory as root, and the user root of the group:
chown -R root:root test
4. Ordinary users change the group chgrp to which the file belongs
Unlike the chown command, chgrp allows ordinary users to change the group to which a file belongs, as long as the user is a member of that group
-
Change the group attribute of the file to staff:
chgrp staff test
-
Change the group of all files in the test directory to staff:
chgrp -R staff test