为什么我不能列出具有读取权限的目录?

wrg*_*grs 15 osx permissions fish

我在其中创建了一个目录d和一个文件f。然后我只给了自己对该目录的读取权限。我明白这应该意味着我可以列出文件(例如这里),但我不能。

will@wrmpb /p/t/permissions> ls -al
total 0
drwxr-xr-x   3 will  wheel  102  4 Oct 08:30 .
drwxrwxrwt  16 root  wheel  544  4 Oct 08:30 ..
dr--------   3 will  wheel  102  4 Oct 08:42 d
will@wrmpb /p/t/permissions> ls d
will@wrmpb /p/t/permissions>
Run Code Online (Sandbox Code Playgroud)

如果我更改写入和执行的权限,我可以看到该文件。

will@wrmpb /p/t/permissions> chmod 500 d
will@wrmpb /p/t/permissions> ls d
f
will@wrmpb /p/t/permissions> 
Run Code Online (Sandbox Code Playgroud)

为什么是这样?我正在使用 MacOS。

编辑:参考@ccorn 的回答,我使用的是鱼并type ls给出以下内容:

will@wrmpb /p/t/permissions> type ls
ls is a function with definition
function ls --description 'List contents of directory'
    command ls -G $argv
end
Run Code Online (Sandbox Code Playgroud)

cco*_*orn 9

一些准备工作,只是为了确保ls不会尝试更多的事情:

$ unalias ls 2>/dev/null
$ unset -f ls
$ unset CLICOLOR
Run Code Online (Sandbox Code Playgroud)

r目录权限演示:

$ ls -ld d
dr--------  3 ccorn  ccorn  102  4 Okt 14:35 d
$ ls d
f
$ ls -l d
ls: f: Permission denied
$ ls -F d
ls: f: Permission denied
Run Code Online (Sandbox Code Playgroud)

在传统的 Unix 文件系统中,目录只是一个(名称,索引节点号)对的列表。inode 编号是一个整数,用作文件系统 inode 表的索引,其中存储了其余的文件元数据。

r对目录的权限允许列出其中的名称,但不能访问存储在 inode 表中的信息,即获取文件类型、文件长度、文件权限等,或打开文件。为此,您需要x目录的权限。

这就是为什么ls -l, ls -F,ls带有颜色编码输出等x未经许可而失败,而仅仅ls成功的原因。

x单独的权限允许 inode 访问,即在该目录中给定一个显式名称,x允许查找其 inode 并访问该目录条目的元数据:

$ chmod 100 d
$ ls -l d/f
-rw-r--r--  1 ccorn  ccorn  0  4 Okt 14:35 d/f
$ ls d
ls: d: Permission denied
Run Code Online (Sandbox Code Playgroud)

因此,打开文件/a/b/c/f或列出它的元数据,目录//a/a/b,和/a/b/c必须被授予x的权限。

不出所料,创建目录条目需要wx权限:

$ chmod 100 d
$ touch d/g
touch: d/g: Permission denied
$ chmod 200 d
$ touch d/g
touch: d/g: Permission denied
$ chmod 300 d
$ touch d/g
$
Run Code Online (Sandbox Code Playgroud)

维基百科在一篇关于文件系统权限的文章中有简要概述。