0

I am observing the following behaviour, and want to know if it is expected.

I am trying to run disk usage ( du ) as user2 on a directory test_directory which is owned by user1.

If I change the permissions to allow everyone full access (using chmod 777 test_directory), then both user1 and user2 are able to properly see the disk usage, as expected:

enter image description here

However, if I restrict executable access for other users (using chmod 776 test_directory), then user2 is not able to run du and a permission error occurs:

enter image description here

In addition, the directory shows as having 4096 bytes size in the case of the error.

  1. Why are executable permissions needed for a user to be able to request the disk usage with du? I would have naively expected that only read permissions are needed (i.e. chmod 774). Actually it seems that both read and execute permissions are needed to run du (i.e. chmod 775).

  2. Why does the directory size default ot 4096 bytes in this case?

Thanks!

teeeeee
  • 157
  • @ArtemS.Tashkinov Not in an obvious way, I'm afraid (but that's down to my beginner linux knowledge). – teeeeee Feb 01 '23 at 11:11

1 Answers1

1

Without the execute/access (x) permission on dir, you can't call stat() on dir/foo.bin, and can't see how large it is. Having just the read (r) permission lets you only list the filenames, but the names are all you get in general.

In some systems, readdir() might give more information than just the names, but I'm not sure if any system can give the file size there. Linux filesystems can give the file type, which never changes during the lifetime of the inode and directory entry. But something constantly varying, like the size is harder to get from just directory in a filesystem where the inodes are separate from the directory listing.

Even if you did get the size with readdir(), just reading dir/ won't give you the size of dir/sub/bar.bin, you'd need to read dir/sub for that. And without access permission on dir, you can't.

The size of a directory is just the size of the list of files, it doesn't include the sizes of the files themselves. So it doesn't tell you much. That 4 kB is common for e.g. ext4, it's just one filesystem block, the minimum the filesystem needs to allocate. Same as with a file, you can get the size with just x permissions on the directory holding it. (e.g. you need x on dir/ to get the size of dir/file.txt or dir/subdir/.)

So yes, you need both read and access permissions to effectively scan the whole directory tree.

See also: Execute vs Read bit. How do directory permissions in Linux work?

ilkkachu
  • 138,973