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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14719403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:28:40  来源:igfitidea点击:

Recursively check ownership of all files

linuxbash

提问by user2045112

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?

最终目标是解决权限问题。但是,我不能得到/*变量去下面的一切/,它只会检查下的文件/在任何新的目录和车站。关于如何检查对所有/子目录及其任何子目录下的权限的任何指示?

采纳答案by criscros

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

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

function playFiles {
    files=
    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 "/root/ " 替换为 : playFiles "/"之前,在另一个目录中播放一点。顺便说一下 playFiles 是一个 bash 函数。希望这会帮助你。

回答by that other guy

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

您可以shopt -s globstar并使用for f in yourdir/**在 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' {} +

回答by JavaRocky

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

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

ls -Rla *