I have created directory a created has these permissions - the other user has
drwxr--r-- 5 user user 4096 2012-09-15 19:30 sites
When do an ls -l on the directory as another user
ls -l /home/user/sites
this is the directory output. I thought without the x bit set on that directory the filenames wouldn't show at all.
d????????? ? ? ? ? ? dev.user.com
-????????? ? ? ? ? ? user.20120914_082804.sql.gz
d????????? ? ? ? ? ? shared
-????????? ? ? ? ? ? shared.tar.gz
-????????? ? ? ? ? ? www.20120914_083256.tar.gz
d????????? ? ? ? ? ? www.user.com
Is there some inconsistency here?
x
gives you permission to actually be in the directory and access the files in the directory,r
gives you permission to see the contents of the directory.If you reversed the situation by giving the directory the
x
bit and removing ther
bit, then the user could openshared.tar.gz
(assuming proper permissions on the file itself) but only if he knew the filename in advance sincels
would be unable to list the files in the directory.This interpretation of permissions dates back to early Unix file systems. In the beginning, there were only files. (Well, and devices, and pipes, and... but I'm trying to tell a story here, not be 100% strictly accurate; besides, its all true for devices and pipes and everything else because everything is a file, even directories).
Directories are just files that the file system uses to hold the metadata that describes the directory tree, and the files it contains. Each file in a directory was described by a simple data structure that contained room for a file name (originally 14 characters, IIRC) along with the inode number where the data was stored, the size of the file, time stamps, and the permissions word. Every directory began with two entries named
.
and..
, the first pointing at the inode of this very directory, and the second at the inode of its parent directory.The permissions word had nine bits to describe the treatment of the owner, other members of the same group, and the world. The three bits for each flag whether the relevant user can read, write, or execute the file. (You might notice that there are five more bits in the 16-bit permissions word that I am ignoring. Those did eventually get meanings assigned but that isn't relevant to this part of the story.) (Also, this interpretation of the nine bits has remained pretty much the same in all descendents of early Unix, including Linux.)
So, if a directory is really just a special kind of file and described by an entry in some directory, it obviously also has permission bits, and those bits probably mean something. But the question is what, exactly. The easiest way to assign meaning to those bits is to not change what they mean in the first place. And that is essentially what was done.
So, the read bit means that the user can read the directory itself. That classically gives the reader access to the file name, time stamps, size, and inode number of each file's data. Specifically, with
r
set you can usels
to see the names of all the files in the directory, but that isn't sufficient to open (or use in any way) any of the listed files.The execute bit means that the user can "execute" the directory. Since directories are special, execute really means look up an entry by name and use it. That means that you can try to open the files if
x
is set, but withoutr
you can't discover their names. Of course, the requested file's permissions also affect access, so even withx
on the directory you won't be able to read a file unless it also offers your
.The write bit means that the user can write to the directory, but naturally only mediated by the file system itself. That means that with
w
set you can create new files in that directory, or edit the directory entries of existing files. But withoutx
set, you can't actually use any files, and withoutr
you can't see them either.As more complicated models of user identity have evolved in Unix and its descendents, these same basic descriptions have managed to stay remarkably unchanged.
In short,
r
means you can see its content,x
means you can use it, andw
means you can modify it even for directories.