user2045112
user2045112

Reputation: 176

Recursively check ownership of all files

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.

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?

Upvotes: 15

Views: 20422

Answers (3)

JavaRocky
JavaRocky

Reputation: 19893

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

ls -Rla *

Upvotes: 10

that other guy
that other guy

Reputation: 123400

You can shopt -s globstar and use for f in yourdir/** to expand recursively in bash4+, or you can use 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' {} +

Upvotes: 35

criscros
criscros

Reputation: 346

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.

Upvotes: 7

Related Questions