Subject: bug#8749: mkdir: feature request --reference

From: Bob Proulx <[email-masked]>
Date: Tue, 31 May 2011 18:29:59 -0600

Shaun Jackman wrote:
> I was using exactly that shell function that you described, but it
> doesn't work for
> 	mkdir -p foo/bar

Since we are talking about shell aliases and functions here is a
simple shell snippet that can create the directories with the modes
explicitly.

  #!/bin/sh
  mkdir_px() {
    case $1 in */*) mkdir_px $(dirname "$1") ;; esac
    test -d "$1" || mkdir -m $(stat -c %a .) "$1"
  }
  mkdir_px "$@"
  exit $?

It is a little bit long but not unduly so.

> I've noticed one issue, which I feel is a bug in mkdir. When using mkdir
> -pm, the specified mode is applied only to the final directory and not
> the parent directories:
> 
> $ mkdir -pm 775 foo/bar
> $ ls -ld foo foo/bar
> drwxr-xr-x 3 sjackman assembly 4096 May 30 10:27 foo
> drwxrwxr-x 2 sjackman assembly 4096 May 30 10:27 foo/bar
> 
> I would expect both foo and foo/bar to have mode 775.

That is the original traditional behavior of 'mkdir -p'.  As Paul
pointed out in his follow-up that behavior has been in use and is now
standardized by POSIX lest some new implementation decides to do
something different from the previously implemented behavior.  It is
unfortunate that it was originally implemented that way but having
done so and gotten into common use it is too late to change it now.
That is why POSIX exists is to prevent the proliferation of different
features on every system when corner case behavior like this is
found.  You can pretty much guarentee that someone is counting on that
behavior if for no other reason than just by accident.

Bob