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 [alias):

if [ -w "$dirname" ]; then
    echo "$dirname is writable"

as the 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"
  • 2
    -1 way too complicated, without being useful enough. A simple [ -w directory ] is enough. – muru Oct 15 '15 at 6:44
  • I know that. Since, OP is new to the forum. I thought to explain it in a simple way. Your solution is more straight forward. – SHW Oct 15 '15 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.