Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
    echo "You are the owner"
    echo "You are NOT the owner"

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

share|improve this question
up vote 14 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
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

Your Answer


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.