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?