当前位置:网站首页>Shell script batch modify file directory permissions
Shell script batch modify file directory permissions
2022-06-28 18:49:00 【51CTO】
background : Through security scanning, it is found that the permissions of many files and directories do not meet the requirements , And put the unqualified files and directories into one file . Now you need to change the permissions of the corresponding files and directories according to the contents of the files
# vim:et:ts=4:sw=4:
#DATE:2021-08-29
#DESCRIBE: Modify file and directory permissions , Store operation log in file_permission.log
#MODIFY:
[
"
$#
"
-ne
1 ] &&
echo
"eg: bash file_permission.sh /root/scan_file.txt" &&
exit
1
FILE_PATH
=
$1
# Security scan result file path
[ !
-f
${FILE_PATH} ] &&
echo
"error:
${FILE_PATH}
non-existent " &&
exit
1
[ !
-f
${FILE_PATH}.bak ] &&
cp
${FILE_PATH}
${FILE_PATH}.bak
dos2unix
${FILE_PATH}
WORKDIR
=
$(cd `dirname
$0
`;pwd)
# Script path
[
-f
${WORKDIR}/file_permission.log ] && >
${WORKDIR}/file_permission.log
# Store the operation log
[
-f
${WORKDIR}/parent_dir.txt ] && >
${WORKDIR}/parent_dir.txt
SUFFIX_NAME
=(h
sh py jar log)
# Found file suffix , omitted “.” Symbol ,grep Will be added when
# Modify the permissions
Modify_Permission()
{
local
suffix_name
=
$1
# file extension
local
file_permission
=
$2
# File permissions
local
dir_permission
=
$3
# The directory permissions
local
file_name
=
$(cat
${FILE_PATH}
|grep
"^-.*\.
${suffix_name}
$"
|awk
'{print $NF}'
)
if [
-n
"
${file_name}
" ];then
# Modify file permissions
for file
in
${file_name}
do
if [
-f
"
${file}
" ];then
echo
"chmod
${file_permission}
${file}
" >>
${WORKDIR}/file_permission.log
chmod
${file_permission}
${file}
fi
done
# Modify the permissions of the directory above the file
for parent_dir
in
${file_name}
do
echo
${parent_dir%/*} >>
${WORKDIR}/file_tmp.txt
done
sort
-r
${WORKDIR}/file_tmp.txt |
sort
-u >>
${WORKDIR}/parent_dir.txt
# Delete the same directory and save it to parent_dir.txt
[
-f
${WORKDIR}/file_tmp.txt ] &&
rm
-rf
${WORKDIR}/file_tmp.txt
for parent_dir
in
$(cat parent_dir.txt)
do
if [
-d
"
${parent_dir}
"
-a
"
${parent_dir}
" !
=
"/tmp"
-a
"
${parent_dir}
" !
=
"/" ];then
echo
"chmod
${dir_permission}
$parent_dir
" >>
${WORKDIR}/file_permission.log
chmod
${dir_permission}
${parent_dir}
local
line
=
$(grep -nw
"
${parent_dir}
$"
${FILE_PATH}
|awk -F
':'
'{print $1}'
)
# Extract the number of rows where the directory path to delete is located
[[
"
${line}
"
-gt
0 ]] &&
sed
-i
"
${line}
d"
${FILE_PATH}
fi
done
# Delete the processed file in the security scan result file
sed
-i
"/\.
${suffix_name}
$/d
"
${FILE_PATH}
echo
"INFO: .
${suffix_name}
File modification completed "
else
echo
"debug: There is no match for .
${suffix_name}
file "
fi
}
###################### The main program ######################
for suffix
in
${SUFFIX_NAME[@]}
do
case
$suffix
in
h|sh|py|jar)
Modify_Permission
"
${suffix}
"
"550"
"550";;
# first 550 Indicates file permissions , the second 550 Indicates the permission of the directory above the file
log)
Modify_Permission
"
${suffix}
"
"640"
"750";;
esac
done
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
边栏推荐
猜你喜欢
随机推荐
POI Excel转换工具
leetcode 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers(最少的“二进制数“个数)
Professor Michael Wooldridge of Oxford University: how the AI community views neural networks in the past 40 years
PMP怎么补考?补考费用是多少?
Business layer modification - reverse modification based on the existing framework
About Critical Values
Sword finger offer 11 Minimum number of rotation array
Shanghai Pudong Development Bank Software Test interview real question
Win 10创建一个gin框架的项目
正版ST-link/V2 J-LINK JTAG/SWD引脚定义和注意事项
About Critical Values
声网 VQA:将实时互动中未知的视频画质用户主观体验变可知
C# 41. int与string互转
What are the design requirements for PCB layout and wiring?
数字化转型的1个目标,3大领域,6大因素和9个环节
Yixin Huachen: real estate enterprises want to grasp the opportunity of the times for digital transformation
19.2 容器分类、array、vector容器精解
【Unity3D】发射(RayCast)物理射线(Ray)
Alist+RaiDrive 给电脑整个80亿GB硬盘
【软件测试】2022年普通高等学校招生全国统一考试









