I'm trying to code a clone of the ls command as part of a school exercise. I'm wondering why the following output is displayed by ls:

$> ls -- -test_dir  /root /root/some-rubbish
ls: /root/some-rubbish: Permission denied

ls: root: Permission denied

Specifically, I've noticed that when the searched folder has more than one "part", that is, when is it has at least one / between two alphanumerical parts, like that/folder, then EACCES errors get displayed at the top. Otherwise, they are displayed in alphabetical order like any other listed directory.

I'm currently testing this on OSX 10.8, if that makes any difference.

I hope the above is clear enough. If not, I'll try to clarify.


The initial set of error messages is produced when ls does an lstat() of each of its arguments. An argument that doesn't exist or that can't be accessed because one or more of its parent directories is non-searchable (doesn't have the appropriate 'x' bit on) are some examples where lstat() will fail and an error message will be produced.

Objects in the argument list for which lstat() fails are removed from the argument list.

After this, each argument has its name printed, and (unless you've given ls the -d option) each argument that is a directory has its name, a ':', and its contents printed. If a directory is not searchable, ls will output an error message at this point. /root is most likely lstat-able but not searchable by non-root users.

| improve this answer | |

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.