这是我第一次尝试bash脚本.我正在尝试创建一个脚本来检查从某个目录下开始的每个文件所有者和组.
例如,如果我有这个:
files=/*
for f in $files; do
owner=$(stat -c %U $f)
if [ "$owner" != "someone" ]; then
echo $f $owner
fi
done
Run Code Online (Sandbox Code Playgroud)
最终目标是修复权限问题.但是,我无法将/*变量放在所有内容之下/,它只会检查下面的文件/并停在任何新目录下.有关如何检查/其下任何子目录及其任何子目录的权限的任何指示?
tha*_*guy 26
您可以shopt -s globstar使用for f in yourdir/**在bash4 +中递归扩展,或者您可以使用find:
find yourdir ! -user someone
Run Code Online (Sandbox Code Playgroud)
如果你想要用户名和文件名相同的输出格式,你必须得到系统特定的:
GNU$ find yourdir ! -user someone -printf '%p %u\n'
OSX$ find yourdir ! -user someone -exec stat -f '%N %Su' {} +
Run Code Online (Sandbox Code Playgroud)
你可以尝试这个,它是一个递归的:
function playFiles {
files=$1
for f in $files; do
if [ ! -d $f ]; then
owner=$(stat -c %U $f)
echo "Simple FILE=$f -- OWNER=$owner"
if [ "$owner" != "root" ]; then
echo $f $owner
fi
else
playFiles "$f/*"
fi
done
}
playFiles "/root/*"
Run Code Online (Sandbox Code Playgroud)
在使用:playFiles"/ " 替换playFiles"/ root/"之前,在另一个目录中播放一些内容.顺便说一下playFiles是一个bash函数.希望这会对你有所帮助.
| 归档时间: |
|
| 查看次数: |
10176 次 |
| 最近记录: |