Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking file owner permissions

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?

like image 405
aport002 Avatar asked Oct 27 '11 00:10

aport002


People also ask

How do I check the owner and file permissions of a file?

Check Permissions in Command-Line with Ls Command If you prefer using the command line, you can easily find a file's permission settings with the ls command, used to list information about files/directories. You can also add the –l option to the command to see the information in the long list format.

What are the file owner permissions?

Unix Command Course for Beginners Owner permissions − The owner's permissions determine what actions the owner of the file can perform on the file. Group permissions − The group's permissions determine what actions a user, who is a member of the group that a file belongs to, can perform on the file.

How do you check the owner of a file in Linux?

A. You can use ls -l command (list information about the FILEs) to find our the file / directory owner and group names. The -l option is known as long format which displays Unix / Linux / BSD file types, permissions, number of hard links, owner, group, size, date, and filename.


1 Answers

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.

like image 171
Chriszuma Avatar answered Sep 21 '22 22:09

Chriszuma