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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With