Linux – Do I correctly understand permission to directories in Linux/Unix

chmodlinuxpermissionsunix

Do I correctly understand permission to directories in Linux/Unix?

  1. If your directory has only r (read) permission you are allowed to see the content of the directory (which files are located there) but you cannot do it because you cannot go (cd) to this directory (because of the absence of x permissions). You also cannot see the content of the directory (which files are located there) from outside of the directory (for example by ls directoryname/*). You also will be unable to read (see) content of files located in such a directory using cat and more commands (even if you have permissions to read these files). You also will be unable to modify (write) files (even if you have write permissions to them) if these files are located in such a directory (no matter what you try cat >>, echo >>, cp or some text editor). So, from my point of view, to have only r permissions to a directory is equivalent to having absolutely no permissions to the directory.

  2. If your directory has only x (execute) permissions you are allowed to go (cd) into the directory but you are not allowed to see (ls) the content of the directory (because you do not have permissions to read the directory). If a directory has only x permission and it contains a file for which you have r (read) and w (write) permissions, you still well be unable to open this file with (at leas some) text editors (for example mcedit). But you will be able to read context of the file using such commands as cat or more. You alls will be able to modify content of the file using echo >> or cat >>. So, it seems to me, that it is x what allows users to "read" and "write" existing files in the directory (if files have the corresponding permissions too).

  3. If a directory has r and x permissions but no w (write) permissions, you cannot change content of the directory (set of files which are located there). For example you cannot create a new file there or remove existing in the directory. But you still are allowed to change content of existing files. So, you need w permissions to create or remove files in the directory.

    Added:

  4. It is also interesting to mention that w permission to the directory is necessary but not sufficient to create and delete files in the directory. If a directory has only w permission you will be unable to add/remove files from/to the directory. To be able to do so, you need to have x permission to the directory (additionally to w permission).

Best Answer

The question says:

If your directory has only "r" (read) permission you are allowed to see the content of the directory (which files are located there) but you cannot do it because you cannot go ("cd") to this directory (because of the absence of "x" permissions).

Yes, you can do it, you can see the list of files that are contained by the directory:

$ mkdir mydir
$ echo text > mydir/myfile
$ chmod a-wx mydir
$ ls -lA
total 4
dr--r--r-- 2 hcs hcs 4096 2010-02-28 22:12 mydir
$ ls -lA mydir
ls: cannot access mydir/myfile: Permission denied
total 0
-????????? ? ? ? ?                ? myfile

But you won't access any other information from the file than its name, as the list shows.

Related Question