文件权限 #
一、权限概述 #
| 权限 | 数字 | 说明 |
|---|---|---|
| r | 4 | 读取 |
| w | 2 | 写入 |
| x | 1 | 执行 |
二、chmod命令 #
bash
#!/bin/bash
# 数字模式
chmod 755 script.sh
chmod 644 file.txt
# 符号模式
chmod +x script.sh
chmod u+rwx,g+rx,o+rx script.sh
chmod a-x file.txt
# 递归修改
chmod -R 755 directory/
三、chown命令 #
bash
#!/bin/bash
# 修改所有者
chown user file.txt
# 修改所属组
chown :group file.txt
# 同时修改
chown user:group file.txt
# 递归修改
chown -R user:group directory/
四、权限检查 #
bash
#!/bin/bash
file="script.sh"
# 检查权限
[ -r "$file" ] && echo "可读"
[ -w "$file" ] && echo "可写"
[ -x "$file" ] && echo "可执行"
# 查看权限
ls -l "$file"
stat "$file"
下一步 #
你已经掌握了文件权限,接下来让我们学习 目录操作!
最后更新:2026-03-27