Checking file owner permissions

僤鯓⒐⒋嵵緔 提交于 2019-12-18 13:54:17

问题


I'm trying to test if a file has the execute bit set for the owner in bash script.

I know if [ -x filename ] checks for execute permission for the User running the statement but i need to know if the owner has it. Is there a way to specify owner?


回答1:


You can use stat to get the file permissions, and parse them with another command to get the character you want.

stat -c %A someFile

Returns something like:

-rw-rw-r--

EDIT: Here you go:

stat -c %A someFile | sed 's/...\(.\).\+/\1/'

Returns either - or x if the owner has execute.

EDIT 2: For completion's sake:

if [ `stat -c %A someFile | sed 's/...\(.\).\+/\1/'` == "x" ] 
then
  echo "Owner has execute permission!"
fi

EDIT 3: If you prefer numerical permissions:

stat -c %a /path/to/a/file will output 600 or 700 or whatever 3 digit base-8 number.



来源:https://stackoverflow.com/questions/7910582/checking-file-owner-permissions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!