Understanding directory permissions in UNIX

bashpermissionterminalunix

I've been trying to learn Unix and Terminal a bit better, and I've come across some behavior I don't understand. Hoping someone can explain to me what I'm missing.

Lately I've been experimenting with permissions on directories. From what I (thought) I understood, read permission means you can view the contents of a directory (i.e., ls directory should list the contents of the directory); write permission means you can create, modify, or delete files in the directory (i.e., touch directory/newfile, or vi directory/fileimade, or rm directory/fileihate, all should work); and execute permission means you can make the directory your working directory (i.e., cd directory should work)

But that isn't what I'm finding.

If I have r-- on a directory, I receive errors on attempting cd directory or touch directory/newfile, as expected. But if I run ls directory — well, I don't get an error, but no files are listed, even if I know there are files that I own and/or have rights to within the directory. So, ls has run successfully but with no data to standard output. Why not?

If I have -w- on a directory, I receive errors on cd and ls, as expected. But if I try to create a new file – touch directory/newfile – I also get an error. Why?

All of the x categories work exactly as expected; I can cd into directories with --x, but nothing else. I can cd into directories with -wx, create and delete files, but ls returns an error. And I can cd into r-x directories, list their contents, and work on files for which I have existing permissions, but cannot create or delete files. All these make sense to me.

So what am I not understanding correctly about r-- and -w-?

Best Answer

If I have r-- on a directory, I receive errors on attempting cd directory or touch directory/newfile, as expected. But if I run ls directory -- well, I don't get an error, but no files are listed, even if I know there are files that I own and/or have rights to within the directory. So, ls has run successfully but with no data to standard output. Why not?

If you just ls directory then the "files" inside directory should list but if you use ls -l which calls stat(2) the operation will silently fail as stat requires a full searchable path to the filesystem object.

If I have -w- on a directory, I receive errors on cd and ls, as expected. But if I try to create a new file - touch directory/newfile - I also get an error. Why?

Once again touch calls open(2) system call which requires a full searchable path (all directories in the path are executable/searchable) or the operation will fail.