check if different user has read/wrte permissions to a file on linux

pollus picture pollus · Mar 24, 2015 · Viewed 13.2k times · Source

How can I check if a specific user with no shell assigned can write or read a file ?

As an example we can use apache user... is there any option in touch or any other commands?

Thanks

Answer

Kevin Burke picture Kevin Burke · Nov 14, 2019

The "test" command is designed for this use case.

sudo -u otheruser test -r /path/to/file

will return 0 if otheruser can read the file, or 1 if otheruser cannot read the file. You can run test -r /path/to/file; echo "$?" to view the return code of the test command.

Use test -w to test for write permission and test -x to test for execute permission.