1

For directories, severals references say:

  • r: we can list directory contents
  • w: we can write on directory
  • x: we can change to directory (cd into directory).

But in my tests I got following situation:

  • my user mateus applies to others permission.
  • x is the directory

Only with r permission for others (chmod 704):

If I issue ls -l in directory I got this crazy output:

mateus@engsrv:/tmp$ ls -l x/
ls: cannot access 'x/file_teste': Permission denied
total 0
-????????? ? ? ? ?            ? file_teste
mateus@engsrv:/tmp$

I can't "cat" the file as well.

mateus@engsrv:/tmp$ cat x/haha
cat: x/haha: Permission denied

Ok, let's se what happen only with w permission for others (chmod 702)

mateus@engsrv:/tmp$ touch x/file_test2
touch: cannot touch 'x/file_test2': Permission denied
mateus@engsrv:/tmp$

If I grant execute (chmod 703), I can write to the folder:

mateus@engsrv:/tmp$ touch x/file_test2

The same happens to read permission (chmod 705):

mateus@engsrv:/tmp$ ls -l x
total 4
-rw-rw-r-- 1 mateus mateus 0 Oct  3 17:45 file_test2
-rw-r--r-- 1 root   root   3 Oct  3 17:31 file_teste

So, execution (x) is always necessary? but why? is there something else that need execution permission in background?

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
Potter
  • 425
  • 4
  • 12
  • Does this answer your question? [Execute vs Read bit. How do directory permissions in Linux work?](https://unix.stackexchange.com/q/21251) – Kusalananda Oct 03 '22 at 18:35
  • You did get the listing of the directory, there's the filename `file_teste` visible in the `ls` output. The thing to note here is that getting the file metadata isn't possible through just reading the directory, but involves accessing the _files_ themselves. That's probably due to historic reasons and the filesystem structure. There's no execution there, read the `x` bit as "access" for directories. – ilkkachu Oct 03 '22 at 19:29

2 Answers2

1

You need to enter (execute) a folder when creating a file (write) in it. Because OS/shell is moving into that folder to create a file. Changing folder is executing it (not directly).

Good point of view to the problem is when you look at folder as a file (in Linux systems) - it is the same as file, but executing it you change your location to inside the folder instead of running (e.g. script, program) it.

But: you can change the folder name even if it does not have write permission (write is for inside of the folder - creating a file). Without execute permission you can't get into folder. If you can execute folder then you can read only or write later inside based on permissions.

EDIT: it was designed this way.

pbies
  • 424
  • 4
  • 15
  • Moving into a directory would usually mean to changing the working directory of the process to said directory, with the `chdir()` system call. But that's not necessary for creating files in the directory, you can just do `open("dir/file")` directly. Both of them need the `x` permission, of course, but calling it "executing the directory" is still silly, even the POSIX text calls it "execute/search" (see e.g. [chmod](https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/chmod.html) but IMO "access" is more apt. – ilkkachu Oct 03 '22 at 19:36
  • @ilkkachu right. I wrote this for OP for idea, not exactly going into tech details. In this case let call it access (let me say "xs" ;). – pbies Oct 03 '22 at 19:39
  • 1
    yes, that's another reason I like the a-word here, spelling it with an x would fit the pronunciation too. – ilkkachu Oct 03 '22 at 19:42
0

Its just because.

You are not executing anything in the background when you change directory. Executing a directory does not really make sense, so it's x bit its free to be interpreted in any which way. This was the way decided. Not because any code is executed. The sys_chdir syscall checks perms but does not execute anything.

teknopaul
  • 693
  • 7
  • 6