Checking directory permissions on UNIX directory

Hi,

How do i check if I have read/write/execute rights on a UNIX directory?

What I'm doing is checking read access on the files but i also want to check if user has rights on the direcory in whcih these files are present.

if [ ! -r ${filename} ] then......

And I check if the directory exists by using this command

if [ ! -d "${dest}" ] then...

Can anyone tell me how do i check if the user has read/write/execute rights on a UNIX directory ?

As well as for regular files.

1 Like

Ok, so if i need to check for all r w x rights can I do it in 1 command line?

if [ ! -rwx ${directory} ] ?

This gives an unary operator expected error. I believe we need to have an and operator ??

Shell is not perl (in the last version of perl you can use these operators in such manner - "-rwx" ). In shell you can use:

if [ ! -r $dir ] && [ ! -w $dir ] && [ ! -x $dir ]
...

Or

$ dir=$HOME
$ test -r $dir -a -w $dir -a -x $dir && echo hi
hi
$ dir=/etc
$ test -r $dir -a -w $dir -a -x $dir && echo hi
$ 

Ohk so here in the shell script i have to check the permissions separately like I have done below.

if [ ! -r ${source} ]
then
echo "Read access permission denied on Source Directory"
exit 1
fi
if [ ! -w ${source} ]
then
echo "Write access permission denied on Source Directory"
exit 1
fi
if [ ! -x ${source} ]
then
echo "Execute access permission denied on Source Directory"
exit 1
fi

kindly let me know if this is the ideal way to check rwx rights or is there any better way to do this...!!

Sorry, I think I was ambiguous. I edited my previous post.

Thanks again :slight_smile: