Subject: bug#8736: chmod -p --parents

From: Jim Meyering <[email-masked]>
Date: Thu, 23 Feb 2012 12:04:22 +0100

Jérémy Compostella wrote:
>> On Thu, May 26, 2011 at 10:13:05PM +0200, francky.leyn <at> telenet.be wrote:
>> >
>> > In the past, I was an advocate of the -p --parents option for
>> > mkdir. By now this is realised. Now I'm doing the same for chmod.
>> > [...]
>> > I want to be able to execute the following:
>> >
>> > chmod a+rx -p ~/dir1/dir2/dir3/file.ext
>>
>> I like the proposal.
>> > It should be more refined however: the dirs
>> > should be rx, but the file only r.
>>
>> You can use a capital X to automatically handle the directory case. But
>> please take a look at the docs for the _exact_ meaning of X.
> I made a first implementation of this chmod --parents feature (see the
> attached patch). I do not support the second part since 'X' is already
> in used for the "Conditional Executability" feature. I have no idea how
> to support it.
>
> I don't have written the documentation for now. I would like to have
> your opinion about this patch first as I maybe missed some corner case
> or the code should be written another way or even something else ...
>
> I really hope you will find time to look at it.

Hi Jérémy,

Thank you for the patch, but as usual with option-adding patches,
we have to justify it, i.e., to ask "why not do it some other way?"
Or, "is this too specialized to merit an option?"

Here's a little bash/zsh script that does it:

chmod-parents()
{
  local p=$2
  while :; do
    chmod "$1" "$p" || break
    test "$p" = . && break
    p=$(dirname "$p")
  done
}

And an example showing how to use it:

  p=d1/d2/d3
  mkdir -p $p
  touch $p/file
  chmod-parents 505 $p/file