递归检查所有文件的所有权

时间:2021-11-08 21:45:13

This if my first attempt at bash scripting. I am trying to create a script to check on every single file owner and group starting under a certain directory.

这是我第一次尝试bash脚本。我正在尝试创建一个脚本来检查从某个目录下开始的每个文件所有者和组。

For example if I have this:

例如,如果我有这个:

files=/*  
for f in $files; do  
    owner=$(stat -c %U $f)  
    if [ "$owner" != "someone"  ]; then  
        echo $f $owner  
    fi  
done

The ultimate goal is to fix permission problems. However, I am not able to get the /* variable to go underneath everything in /, it will only check the files under / and stop at any new directories. Any pointers on how I could check for permissions over everything under / and any of its sub-directories?

最终目标是修复权限问题。但是,我无法将/ *变量放在/中的所有内容之下,它只会检查/下的文件并在任何新目录中停止。有关如何检查/及其任何子目录下的所有权限的任何指示?

3 个解决方案

#1


5  

you can try this one, it is a recursive one:

你可以尝试这个,它是一个递归的:

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/*"

Play a little with in a another directory before replacing playFiles "/root/" with : playFiles "/". Btw playFiles is a bash function. Hopefully this will help you.

在使用:playFiles“/”替换playFiles“/ root /”之前,在另一个目录中播放一些内容。顺便说一下playFiles是一个bash函数。希望这会对你有所帮助。

#2


24  

You can shopt -s globstar and use for f in yourdir/** to expand recursively in bash4+, or you can use find:

你可以购买-s globstar并在yourdir / **中使用f来在bash4 +中递归扩展,或者你可以使用find:

find yourdir ! -user someone

If you want the same output format with username and filename, you have to get system specific:

如果您想要与用户名和文件名相同的输出格式,您必须获得系统特定的:

GNU$  find yourdir ! -user someone -printf '%p %u\n' 
OSX$  find yourdir ! -user someone -exec stat -f '%N %Su' {} +

#3


2  

List all files recursively in list format and hidden files which shows ownership and permissions

以列表格式递归列出所有文件,显示所有权和权限的隐藏文件

ls -Rla *

#1


5  

you can try this one, it is a recursive one:

你可以尝试这个,它是一个递归的:

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/*"

Play a little with in a another directory before replacing playFiles "/root/" with : playFiles "/". Btw playFiles is a bash function. Hopefully this will help you.

在使用:playFiles“/”替换playFiles“/ root /”之前,在另一个目录中播放一些内容。顺便说一下playFiles是一个bash函数。希望这会对你有所帮助。

#2


24  

You can shopt -s globstar and use for f in yourdir/** to expand recursively in bash4+, or you can use find:

你可以购买-s globstar并在yourdir / **中使用f来在bash4 +中递归扩展,或者你可以使用find:

find yourdir ! -user someone

If you want the same output format with username and filename, you have to get system specific:

如果您想要与用户名和文件名相同的输出格式,您必须获得系统特定的:

GNU$  find yourdir ! -user someone -printf '%p %u\n' 
OSX$  find yourdir ! -user someone -exec stat -f '%N %Su' {} +

#3


2  

List all files recursively in list format and hidden files which shows ownership and permissions

以列表格式递归列出所有文件,显示所有权和权限的隐藏文件

ls -Rla *