“ls”即使有效,也会给出“权限被拒绝”错误?

use*_*947 6 linux permissions

我有一个具有r权限位的目录:

dr-------- 3 robert robert 4096 2017-12-17 03:47 dir
Run Code Online (Sandbox Code Playgroud)

该目录有两个文件和一个目录:

file 1.txt  file 2.txt  subdir
Run Code Online (Sandbox Code Playgroud)

当我运行命令ls dir(从robert帐户)时,我在目录内容旁边收到一个错误:

ls: cannot access dir/file 1.txt: Permission denied
ls: cannot access dir/file 2.txt: Permission denied
ls: cannot access dir/subdir: Permission denied
file 1.txt  file 2.txt  subdir
Run Code Online (Sandbox Code Playgroud)

为什么ls即使显示目录的内容也没有任何问题却显示错误?

ilk*_*chu 12

ls在目录本身上调用readdir()(或getdents()),这相当于“读取”目录。我认为一些 Unix 将其实现为对 的实际调用read(),就好像目录是一个文件一样。这是有效的,因为您对该目录具有读取权限,但它只给出了文件的名称。

除此之外,ls经常想找到文件的类型(用不同的颜色显示不同的类型,或者把尾部的斜杠和星号等放在 中ls -F)。但是找到类型通常需要调用stat()文件本身,这需要+x对目录的访问 ( ) 权限,与实际打开文件所需的方式相同。而你没有。