Recursively check ownership of all files

This is if my first attempt to create a bash script. I am trying to create a script to check each file owner and group, starting from a specific 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 issues. However, I cannot force the /* variable to go under everything in / , it will only check files in / and stop in any new directories. Any pointers to how I can check permissions for everything under / and any of its subdirectories?

+4
source share
3 answers

you can try this, it is recursive:

 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 different directory before replacing playFiles "/ root /" with: playFiles "/". Btw playFiles is a bash function. Hope this helps you.

+4
source

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

 find yourdir ! -user someone 

If you want to have the same output format with the username and file name, you need to get the system information:

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

List all files recursively in list format and hidden files that show ownership and permissions

 ls -Rla * 
+2
source

All Articles