[Bash] chmod and chown

Zhentiw發表於2024-05-19

Understanding File Permissions:
File permissions in Unix-like systems determine who can read, write, or execute a file. They are represented as a combination of three groups: user (u), group (g), and others (o).

-rwxr-xr--

r stands for read permission.
w stands for write permission.
x stands for execute permission.

chmod

chmod: Change file permissions.

chmod 755 filename

touch testfile
ls -l testfile

# Change the file permissions to make it executable by the owner and readable by everyone:
chmod 744 testfile
ls -l testfile

# Change the file permissions using symbolic notation:
chmod u+x,g+r,o=r testfile
ls -l testfile

Explain

Explanation of chmod u+x,g+r,o=r testfile
The chmod command is used to change the permissions of a file or directory. The syntax of the command includes specifying the users (user, group, others), the operation (add, remove, set), and the permissions (read, write, execute).

u stands for "user" (the owner of the file).

g stands for "group" (the group to which the file belongs).

o stands for "others" (everyone else).

  • means adding a permission.

= means setting a permission explicitly.

r stands for "read" permission.

w stands for "write" permission.

x stands for "execute" permission.

Command Breakdown:
u+x: Add execute permission for the user (file owner).
g+r: Add read permission for the group.
o=r: Set read permission for others (removes any other permissions for others).

Chown

chown: Change file owner and group.

chown [owner][:group] file

# Change the owner of testfile to yourusername:
chown $USER testfile
# > -rwxr--r--  1 zhentianwan  staff  0 May 19 10:35 testfile

# Change the owner and group of testfile:
chown $USER:Staff testfile

相關文章