I want to loop recursively through every file and directory below an specified path and echo that file/directory's owner and name.
I found this question to be a good reference, but I would like to know if there is a bash 3.2
portable and simpler solution to the problem. A solution without using find
would be excellent.
I also discovered there is shopt -s globstar
, but I don't know whether it is portable or not.
If you look at my script, you will see that log_owners
simply loops over $@
, so maybe the problem is in my glob pattern. I thought log_owners $root/**
would loop through everything, but it didn't.
# OSX check.
darwin() {
[ $(uname) = "Darwin" ]
}
# Get file/directory's owner.
owner() {
if [ -n "$1" ]; then
if darwin; then
stat -f "%Su" "$1"
else
stat -c "%U" "$1"
fi
fi
}
log_owners() {
for file in $@; do
echo $(owner "$file")"\t$file"
done
}
I am using sh
/bash 3.2
.