2

From https://unix.stackexchange.com/a/350629/674

For directories, the execute permission is a little different; it means you can do things to files within that directory (e. g. read or execute them).

cd into a directory needs execution permission, but does it do something to some file in the directory, and if yes, how? The best I can think of is cd do something to the file . under the directory, but why doesn't cd just deal with the directory itself, instead of any file under it, so as to avoid needing execution permission?

Thanks.

Tim
  • 98,580
  • 191
  • 570
  • 977

1 Answers1

6

On a directory, the execute permission is known as the search permission. It is required in order to access a directory, in a general sense: access files inside the directory, as in the quote above, but also access the directory itself.

cd uses chdir, which is defined as requiring search permission on all components in a path it’s given (see EACCES there).

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
  • Thanks. Just to make sure that I understand your replies. `cd` needs execution permission of the directory which it applies to, because `cd` calls system call function `chdir()` which requires it. When access a file under a directory, why do we need the execution permission of the directory? Is there some system call function which requires it? – Tim Apr 26 '18 at 13:16
  • Yes, [`open`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html) (look for `EACCES` there too). [`path_resolution(7)`](http://man7.org/linux/man-pages/man7/path_resolution.7.html) is also worth reading. – Stephen Kitt Apr 26 '18 at 13:25
  • Why is reading the filenames for the files under a directory not "access the directory" ? What does "access the directory" mean? – Tim Apr 26 '18 at 17:18
  • Enter it (make it your current directory), or traverse it (on your way to a sub-directory). Reading the filenames requires reading the directory, not necessarily accessing it (you can short-`ls` a directory you can’t access). – Stephen Kitt Apr 26 '18 at 17:37