Linux: Use stat to verify permissions and ownership

When troubleshooting an issue where ownership and permissions of a file or directories is questioned, we often go straight to chown/chmod and recursively set these values.

However, this touches every file so if you are still in investigative mode it would be better to use “stat” to simply check the ownership and permissions first.  If you need to recursively check the ownership of every file in a subdirectory:

find . -type f | xargs stat -c "%U:%G %n"

And if you want to see this same list, but only the files who are NOT owned by the user “myuser” in the group “mygroup”:

find . -type f | xargs stat -c "%U:%G %n" | grep -v "myuser:mygroup"

The same thing can be applied to permissions, if you want to view the octal permissions of every subdirectory:

find . -type d | xargs stat -c "%a %n"

And if you want to see only the the directories that do NOT have the permissions 775:

find . -type d | xargs stat -c "%a %n" | grep -v "775"

 

REFERENCES

stat man