How to check if a user can access a given file?
To verify access visually, you can use
namei -m /path/to/really/long/directory/with/file/in
which will output all of the permissions in the path in a vertical list.
or
namei -l /path/to/really/long/directory/with/file/in
to list all owners and the permissions. Other answers explain how to verify this programmatically.
You can use bash to do this.
$ cat check-permissions.sh
#!/bin/bash
file=$1
# Handle non-absolute paths
if ! [[ "$file" == /* ]] ; then
path=.
fi
dirname "$file" | tr '/' $'\n' | while read part ; do
path="$path/$part"
# Check for execute permissions
if ! [[ -x "$path" ]] ; then
echo "'$path' is blocking access."
fi
done
if ! [[ -r "$file" ]] ; then
echo "'$file' is not readable."
fi
$ ./check-permissions.sh /long/path/to/file.txt
To check this for a specific user, you can use sudo
.
sudo -u joe ./check-permissions.sh /long/path/to/file.txt
If you have root access, impersonate the user, then run test -r
(read), test -w
(write), or test -x
(execute) to check whether the user can read/write/execute the given file.
sudo -u otheruser test -w /file/to/test || {
echo "otheruser cannot write the file"
exit 1
}