It does look like mkdir
doesn't apply the mode set by -m
when it creates the intermediary directories. But, at least in most cases, you can use the umask
to modify the permissions it sets for them.
The umask
works by clearing the permission bits that are set in the umask, so if you want the intermediaries to have e.g. the permissions 0700
, set umask to 0077
. Using a subshell here to contain the umask change:
$ ( umask 0077; mkdir -p a/b )
$ ls -ld a
drwx------ 3 ilkkachu ilkkachu 4096 Jan 7 07:23 a/
$ ls -ld a/b
drwx------ 2 ilkkachu ilkkachu 4096 Jan 7 07:23 a/b/
Similarly, to get the permissions 0555
, you'd set umask to 0222
. But this doesn't actually work for the intermediaries, they are created with permissions 0755
instead. That is, the write permission for the owning user is added.
POSIX specifies for the permissions of the intermediate pathname components (see under the -p
option):
[...] the
mkdir
utility shall create any pathname components of the path prefix of dir that do not name an existing directory [by equivalent of creating the missing directory]
and then calling the chmod()
function with the following arguments: [...]
2. The value (S_IWUSR|S_IXUSR|~filemask)&0777
as the mode argument, where filemask
is the file mode creation mask of the process.
(S_IWUSR
and S_IXUSR
are the C constants for those permissions bits.)
The GNU coreutils online manual says:
-p
, --parents
Make any missing parent directories for each argument, setting their file permission bits to =rwx,u+wx
, that is, with the umask modified by u+wx
. Ignore existing parent directories, and do not change their file permission bits.
It goes on to say that the -m
option doesn't apply to these, but that you can hence use the umask to control the permissions of them.
(Though the text there looks off, it mentions "The umask must include u=wx
for this method to work", which seems to be missing the inverted sense of the umask.)
Presumably mkdir
does that because otherwise it wouldn't be able to create the next directory inside the one just created without write permissions. The same happens for the access / search (x
) permission bit, for the same reason.
So, if you want to create directories you can't write to or search yourself, it looks like you'll just have to chmod
them after. Otherwise, setting umask
for the mkdir
works.
mkdir
– Bravo Jan 06 '22 at 22:57umask
setting for the intermediates, but forcesumask
to022
. I do wonder if that counts as a bug though... – ilkkachu Jan 06 '22 at 23:31umask
, but555
is not settable per umask, it's preventable, at best. – Marcus Müller Jan 06 '22 at 23:35555
how a random number how example in this question, it could be other value such as770
– Manuel Jordan Jan 06 '22 at 23:41umask
"fail" just with555
? – Manuel Jordan Jan 07 '22 at 00:02mkdir -p
andinstall -d
? – steeldriver Jan 07 '22 at 00:11