Why ls without -l return 1 when permission to directory is r–

directorylspermissions

I have read this and this, and found that my problem is different and more specific.

I understand the following points.

  • +x on the directory grants access to files inodes through this specific directory
  • meta information of a file, which is used by ls -l, is stored in its i-node, but file name does not belong to that

From the 2 points above, since ls without -l does not need to access the i-nodes of the files in the directory, it should successfully list the file names and return 0.

However, when I tried it on my machine, the file names are listed, but there were some warnings like permission denied, and the return code is 1.

b03705028@linux7 [~/test] chmod 500 permission/
b03705028@linux7 [~/test] ls --color=no permission/
f1*
b03705028@linux7 [~/test] chmod 400 permission/
b03705028@linux7 [~/test] ls --color=no permission/
ls: 無法存取 'permission/f1': 拒絕不符權限的操作
f1
b03705028@linux7 [~/test] echo $0
bash

The Chinese characters basically talk about permission denied

My unix distribution is Linux 4.17.11-arch1

Best Answer

I suspect ls in your case is an alias to something like ls --color=auto; in that case, ls tries to find information about the files contained inside the directory to determine which colour to use.

ls --color=no

should list the directory without complaining.

If it still complains, then you may be using another option, like -F or --classify, that needs to access file metadata (-F/--classify looks at the file type, for example).

To be sure that you run ls without going through an alias, use either of

command ls

or

\ls

To remove an alias for ls, use

unalias ls
Related Question