使用shell批量修改目录权限

时光煮酒 / 2023-08-16 / 原文

背景:

做FTP服务器数据迁移,要保证FTP能正常写入文件目录需要有755权限,从安全考虑文件仅需要要644权限,由于文件层级较多,使用chmod -R命令来修改目录和文件只能修改为同一种权限,不能满足需求.

过程

  • 先将所有文件目录权限修改为644
chmod -R 664 folder_path
  • 执行Shell脚本
#!/bin/bash
#########################################
#Function:    folder_chmod.sh
#Usage:       bash
#########################################
input_path=$1
output_file="/root/folder_list.txt"

if [ ! $input_path ]; then
  echo '请输入目录'
  exit
fi

find $input_path -type d > $output_file

while read -r folder; do
  echo $folder
  #修改目录权限
  chmod 755 $folder
done < $output_file
  • 执行脚本
sh folder_chmod.sh /data/ftp_store