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?
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.
To change group ownership of a directory and all of the files and subdirectories in that directory, use chgrp recursively.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With