Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I know you can determine the owner of directory by doing:

ls -ld ~/foo | awk '{ print $3 }'

You could then compare it to the current user by doing this:

if [[ $(ls -ld ~/foo | awk '{ print $3 }') == "$USER" ]] # or $(id -u -n ) instead of $USER
then
    echo "You are the owner"
else
    echo "You are NOT the owner"
fi

But you can have permissions to write without being the owner. How do you determine this?

share|improve this question
add comment

1 Answer

up vote 11 down vote accepted

I suppose

if [ -w ~/foo ]; then ....

should do what you want.

Also, stat -c %U ~/foo is a better way to obtain the owner than parsing ls output.

share|improve this answer
1  
Note that stat is specific to modern Linux systems, other unices may have a different stat or none at all. Parsing ls is usually recipe for trouble, but parsing the owner as the third word mostly works (things break down afterwards); it's still not perfect (some systems allow spaces in user names). –  Gilles Aug 19 '11 at 13:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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