Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively check ownership of all files

Tags:

linux

bash

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?

like image 305
user2045112 Avatar asked Feb 06 '13 00:02

user2045112


People also ask

How do you run chown recursively?

The easiest way to use the chown recursive command is to execute “chown” with the “-R” option for recursive and specify the new owner and the folders that you want to change.

Which command used to recursively change the ownership of an entire directory tree?

To change group ownership of a directory and all of the files and subdirectories in that directory, use chgrp recursively.

How do I check recursive permissions in Linux?

Try any one of the following commands to see recursive directory listing: ls -R : Use the ls command to get recursive directory listing on Linux. find /dir/ -print : Run the find command to see recursive directory listing in Linux. du -a . : Execute the du command to view recursive directory listing on Unix.


1 Answers

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.

like image 125
criscros Avatar answered Nov 01 '22 21:11

criscros