Linux: ls -l 只打印问号:

osg*_*sgx 11 linux ls coreutils

我在使用 ls -l 列出某个目录时遇到问题:

$ ls -l ./directory
-????????? ? ? ? ?            ? file001.txt
-????????? ? ? ? ?            ? file002.txt
Run Code Online (Sandbox Code Playgroud)

只是 ls 效果很好:

$ ls ./directory
file001.txt file002.txt
Run Code Online (Sandbox Code Playgroud)

怎么了?

Bat*_*hyX 15

检查权限./directory:如果您对该目录具有读取权限但没有执行权限,则您有足够的权限读取该目录中的文件列表,但您实际上无法使用这些文件或获取有关它们的信息。

示例会话:

$ cd /tmp/
$ mkdir /tmp/test
$ touch /tmp/test/file
$ ls -la test/
total 44
drwxr-xr-x  2 myself myself  4096 janv.  5 11:01 .
drwxrwxrwt 42 root   root   54242 janv.  5 11:01 ..
-rw-r--r--  1 myself myself     0 janv.  5 11:01 file
$ chmod a-x /tmp/test # remove execute permission for all
$ ls -la test/
total 0
d????????? ? ? ? ?            ? .
d????????? ? ? ? ?            ? ..
-????????? ? ? ? ?            ? file
$ ls -ld test/
drw-r--r-- 2 myself myself 4096 Jan  5 11:01 test/
$ cat test/file 
cat: test/file: Permission denied
$ chmod a+x /tmp/test # readd execute permission for all
$ ls -la test/
total 44
drwxr-xr-x  2 myself myself  4096 janv.  5 11:01 .
drwxrwxrwt 42 root   root   54242 janv.  5 11:01 ..
-rw-r--r--  1 myself myself     0 janv.  5 11:01 file
$ ls -ld test/
drwxr-xr-x 2 myself myself 4096 Jan  5 11:01 test/
$ cat test/file
$
Run Code Online (Sandbox Code Playgroud)

某些ls版本在无法显示有关文件的信息时会显示错误消息。