当前位置:网站首页>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.
边栏推荐
- 模块化操作
- What are the design requirements for PCB layout and wiring?
- Modular operation
- Baidu time factor addition
- 【Unity3D】相机跟随
- Sword finger offer 11 Minimum number of rotation array
- 今天开户今天能买股票吗?在线开户是安全么?
- Go, begin, end, for, after, instead of
- Sharing-JDBC分布式事务之Seata实现
- Konva series tutorial 3: Customizing drawings
猜你喜欢
![[unity3d] emission (raycast) physical ray (Ray)](/img/46/a9fda743f597db9584c982b10c191c.png)
[unity3d] emission (raycast) physical ray (Ray)

PHP使用栈解决迷宫问题

Summary of the use of qobjectcleanuphandler in QT

从理论到实践增强STEAM和工程教育

Yixin Huachen: real estate enterprises want to grasp the opportunity of the times for digital transformation

C# 41. Int to string

Tensorboard Usage Summary

从知名软件提取出的神器,吊打一众付费

Anonymous function this pointing and variable promotion

Cannot read property 'MTJ' of undefined
随机推荐
Database comparison tool
1 invalid import format(s) Postman Collection Format v1 is no longer supported and can not be import
CVPR2022 | 浙大、蚂蚁集团提出基于标签关系树的层级残差多粒度分类网络,建模多粒度标签间的层级知识
声网发布灵隼物联网云平台 可一小时构建示例场景
双功能交联剂丨Lumiprobe 磺基花青7二羧酸研究
render函数解析
从理论到实践增强STEAM和工程教育
FFmpeg学习总结
[unity3d] emission (raycast) physical ray (Ray)
记一次Emotet木马处理案例
id门禁卡复制到手机_怎么把手机变成门禁卡 手机NFC复制门禁卡图文教程
BioVendor游离轻链(κ和λ)Elisa 试剂盒检测步骤
牛津大學教授Michael Wooldridge:AI社區近40年如何看待神經網絡
324. swing sequencing II
Native implementation Net5.0+ custom log
运筹学note
SqlTransaction
Collection of real test questions
基于管线的混合渲染
Openfire用户以及群组关系移植