本文目录导读:

使用 find + chmod 命令(最常用)
批量修改目录为755,文件为644
#!/bin/bash
# 目标目录
target_dir="/path/to/your/directory"
# 修改目录权限为755
find "$target_dir" -type d -exec chmod 755 {} \;
# 修改文件权限为644
find "$target_dir" -type f -exec chmod 644 {} \;
批量修改特定类型文件
#!/bin/bash
# 只修改 .sh 脚本文件为可执行
find . -name "*.sh" -exec chmod +x {} \;
# 修改所有 .py 文件为755
find . -name "*.py" -exec chmod 755 {} \;
递归批量修改脚本
#!/bin/bash
# 递归批量修改文件权限
batch_chmod() {
local dir="$1"
local dir_perm="$2" # 目录权限
local file_perm="$3" # 文件权限
if [ ! -d "$dir" ]; then
echo "错误: 目录 $dir 不存在"
exit 1
fi
echo "开始处理目录: $dir"
# 修改目录权限
find "$dir" -type d -exec chmod "$dir_perm" {} \;
# 修改文件权限
find "$dir" -type f -exec chmod "$file_perm" {} \;
echo "完成!目录权限: $dir_perm, 文件权限: $file_perm"
}
# 使用示例:修改当前目录及其子目录
batch_chmod "/path/to/dir" "755" "644"
交互式批量修改脚本
#!/bin/bash
echo "===== 批量文件权限修改工具 ====="
# 获取目标路径
read -p "请输入目标目录路径: " target_dir
if [ ! -d "$target_dir" ]; then
echo "错误: 目录不存在!"
exit 1
fi
echo "请选择修改模式:"
echo "1) 标准模式 (目录755, 文件644)"
echo "2) 可执行模式 (所有文件755)"
echo "3) 限制模式 (目录700, 文件600)"
echo "4) 自定义权限"
read -p "请选择 (1-4): " mode
case $mode in
1)
dir_perm="755"
file_perm="644"
;;
2)
dir_perm="755"
file_perm="755"
;;
3)
dir_perm="700"
file_perm="600"
;;
4)
read -p "请输入目录权限 (如755): " dir_perm
read -p "请输入文件权限 (如644): " file_perm
;;
*)
echo "无效选择!"
exit 1
;;
esac
# 预览将要修改的文件数量
dir_count=$(find "$target_dir" -type d | wc -l)
file_count=$(find "$target_dir" -type f | wc -l)
read -p "将修改 $dir_count 个目录和 $file_count 个文件,确认执行? (y/n): " confirm
if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
# 执行修改
find "$target_dir" -type d -exec chmod "$dir_perm" {} \;
find "$target_dir" -type f -exec chmod "$file_perm" {} \;
echo "修改完成!"
else
echo "已取消操作"
fi
高级功能脚本
#!/bin/bash
# 高级批量权限修改工具
usage() {
echo "用法: $0 [选项] <目录>"
echo "选项:"
echo " -d <权限> 设置目录权限 (默认: 755)"
echo " -f <权限> 设置文件权限 (默认: 644)"
echo " -e 仅对可执行文件操作"
echo " -s <后缀> 仅对指定后缀文件操作"
echo " -v 显示详细信息"
echo " -h 显示帮助信息"
echo "示例:"
echo " $0 /path/to/dir"
echo " $0 -d 700 -f 600 /path/to/private"
echo " $0 -s sh /path/to/scripts"
exit 0
}
# 默认参数
dir_perm="755"
file_perm="644"
verbose=false
extension=""
executable_only=false
# 解析参数
while getopts "d:f:e:s:vh" opt; do
case $opt in
d) dir_perm="$OPTARG" ;;
f) file_perm="$OPTARG" ;;
e) executable_only=true ;;
s) extension="$OPTARG" ;;
v) verbose=true ;;
h) usage ;;
*) usage ;;
esac
done
shift $((OPTIND-1))
target_dir="${1:-.}"
if [ ! -d "$target_dir" ]; then
echo "错误: 目录 '$target_dir' 不存在"
exit 1
fi
echo "开始处理目录: $target_dir"
echo "目录权限: $dir_perm"
echo "文件权限: $file_perm"
# 处理目录
if [ "$verbose" = true ]; then
find "$target_dir" -type d -print -exec chmod "$dir_perm" {} \;
else
find "$target_dir" -type d -exec chmod "$dir_perm" {} \;
fi
# 处理文件
if [ "$executable_only" = true ]; then
# 仅处理可执行文件
find "$target_dir" -type f -executable -exec chmod "$file_perm" {} \;
elif [ -n "$extension" ]; then
# 仅处理指定后缀文件
find "$target_dir" -type f -name "*.$extension" -exec chmod "$file_perm" {} \;
else
# 处理所有文件
find "$target_dir" -type f -exec chmod "$file_perm" {} \;
fi
echo "处理完成!"
# 显示统计信息
if [ "$verbose" = true ]; then
dir_count=$(find "$target_dir" -type d | wc -l)
file_count=$(find "$target_dir" -type f | wc -l)
echo "处理了 $dir_count 个目录和 $file_count 个文件"
fi
常用场景示例
Web 服务器权限设置
#!/bin/bash
# 为网站目录设置安全权限
web_root="/var/www/html"
# 目录权限:755 (rwxr-xr-x)
find "$web_root" -type d -exec chmod 755 {} \;
# 文件权限:644 (rw-r--r--)
find "$web_root" -type f -exec chmod 644 {} \;
# 可执行脚本:755
find "$web_root" -name "*.cgi" -o -name "*.pl" -exec chmod 755 {} \;
一键修复权限
#!/bin/bash
# 快速修复常见文件权限问题
fix_permissions() {
local dir="$1"
echo "修复 $dir 的权限..."
# 设置目录权限
find "$dir" -type d -exec chmod 755 {} \;
# 设置文件权限
find "$dir" -type f -exec chmod 644 {} \;
# 特殊目录设置
if [ -d "$dir/uploads" ]; then
chmod 755 "$dir/uploads"
fi
if [ -d "$dir/cache" ]; then
chmod 777 "$dir/cache"
fi
echo "权限修复完成!"
}
# 使用示例
fix_permissions "/path/to/your/project"
使用建议
- 测试先行:在大规模修改前,先用
find的-print选项预览将要修改的文件 - 备份重要数据:修改敏感目录前先备份
- 权限规范:
- 目录通常用 755(安全考虑可用 750)
- 普通文件用 644(脚本文件用 755)
- 敏感配置文件用 600
这些脚本可以根据实际需求调整使用。