How to check user has write permission to the directory,the directory name is passed as a command line argument.
The answer from @SHW is not a good idea and it is returning incorrect results in some cases.
Please just use the
test command (or it's
if [ -w "$dirname" ]; then echo "$dirname is writable" fi
test command with its
-w test will not check the historical
UNIX permission bits but rather call
access(2) to verify writability.
The advantage of using
access(2) is that
access also includes
ACL influences that are missing if you just check the traditional UNIX permission bits.
Simple script to find out the write permission
ls -ld directory | cut -c -10 > temp_perm owner_perm=`cut -c 3 temp_perm` group_perm=`cut -c 6 temp_perm` other_perm=`cut -c 9 temp_perm` [ "$owner_perm" -eq "w" ] && echo "Owner have write permission" [ "$group_perm" -eq "w" ] && echo "Group have write permission" [ "$other_perm" -eq "w" ] && echo "Other have write permission"