7

In fact, I would like to ask more general question -- "what does write permission for a directory allow you to do exactly?" -- but let's approach it with a concrete example.

It is a long question, if you are in a hurry read the bold -- it should cover the main part.

Different sources (nice question, one more, grymoire's) say something similar to the following on the directory permissions:

r, read -- reading of the directory's content (filenames inside)

w, write -- changing the directory's attributes (e.g. modification time) and creating/renaming/removing entries inside

x, search -- accessing files inside, you have access to the inode of the file, hence you can reach it's actual content

My problem is with the description of w. What directory's attributes does it give you access to? I cannot create/rename/remove a file inside a directory with only write permission: I make a directory (tdir/) and a file inside (afile), chmod -x-r tdir/, mv tdir/afile tdir/af, rm tdir/afile, touch tdir/newfile -- all fail with permission denial, unless I set x permission to the directory as well.

And x alone doesn't give you the permission to create/rename/remove files inside the directory.

In order to do that you need both x and w.

But touch tdir does change the modification time of the directory with w only.

I would rephrase the sources above this way for the compliance with the issue: a directory's r allows you to see the filenames inside, but no access to the actual file (to the inode); x gives you access to the inodes of the files (which means you can see their permissions and, according to it, have access to the contents), but you still cannot change anything in the directory; directory is actually some sort of a file and to change something in it you need the w permission.

Thus, when you are changing something in the directory you need w permission. If your change requires inodes of the files in the directory -- you need x as well.

It explains why you cannot remove a file inside a directory with w only: when removing a file you need to reduce the link count of the inode by 1 -- you need to know the inode -- thus you need x for the directory.

But why do you need x for creating (you could ask the system to create a file without exposing the inode?) and renaming/moving the file (when you move a file you don't change it in any way, you only change the records inside the directories and their inode counts?)?

Maybe it is just an implementation thing? I.e. indeed you don't need the inode for renaming/creating files -- you need only filenames and w permission; but inode and filename constitute one record in the directory; thus changing the filenames = changing the records = kind of accessing the inodes.

And also what attributes do directories have besides modification time, permissions and files records? What else in the directory can you change with w only?

1

1 Answer 1

6

x gives you access to the inodes of the files (which means you can see their permissions and, according to it, have access to the contents), but you still cannot change anything in the directory; directory is actually some sort of a file and to change something in it you need the w permission.

Yes.

But why do you need x for creating (you could ask the system to create a file without exposing the inode?) and renaming/moving the file (when you move a file you don't change it in any way, you only change the records inside the directories and their inode counts?)?

Without x, you can only affect the directory itself — you're seeing the directory from the outside. Without x, the directory entries are out of bounds for you. If you want to add, remove, or modify (e.g. rename) an entry in the directory, you need to be able to access that entry.

The permissions on a file determine what you can do with the file's content. The permissions on the directory determine what you can do with the directory entry for the file, since the directory entries are the directory's content.

Write permission in a directory lets you create and remove entries. Renaming counts as atomically creating an entry and removing another one. Beyond that, directories have the same metadata as regular files. Write permission also lets you change the directory's last modification and last access timestamps. To change a directory's permissions, group ownership or access control lists (where supported), you need to own it. To change its user ownership, most unix variants require root.

1
  • thanks Gilles - this is perfecto - succinct, accurate, and understandable. weird that it has languished. This should be in the tag wiki, I think.
    – mikeserv
    Commented Sep 11, 2014 at 14:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .