Linux – How to set file permissions so that new files inherit same permissions?

linuxpermissionsUbuntu

I have a folder in which new subfolders and files will be created automatically, by a script.

I want to maintain the user and group permissions recursively for all new folders and files placed in the parent directory. I know this involves setting a sticky bit, but I can't seem to find a command that shows exactly what I need.

This is what I have done so far:

sudo mkdir -p /path/to/parent
sudo chmod -R 660 myself:somegroup /path/to/parent

Thereafter, I want the 660 permissions to be set recursively to any folders and files placed in /path/to/parent.

However, everything I have tried so far has failed. Can someone help please?

Actually the octal flag 660 is probably not even correct. The permissions I want are:

  1. Directories placed under /path/to/parent are eXecutable by users with permissions
  2. files are read/writeable by user myself and members of somegroup
  3. Files and folders in /path/to/parent is NOT world readable

I am running on Ubuntu 10.0.4 LTS.

Can someone help please?

Best Answer

Grawity gives an excellent answer but I suspect the edited question may have changed things slightly.

I would suggest leaving the directory owned by the apache user/group. This will probably be either apache or httpd depending on your distribution.

e.g.

chown -R apache:apache /path/to/parent

You can then do something like https://serverfault.com/questions/164078/is-adding-users-to-the-group-www-data-safe-on-debian or even add yourself to the apache group to ensure you have group access to the directory. (Something like usermod -aG apache username)

I would not chmod -R the entire directory because you don't want html scripts or jpg's or random other things executable. You should change permissions as required. (though resetting it to 660 may not be the worst of ideas.)

Something you may like to try is:

chmod o+w file

The 'o' means 'other' & 'w' means 'write'. You can also have 'u' for 'user' & 'g' for 'group', as well as 'r' & 'x' which are hopefully self explanatory. You can remove permissions using '-' rather than '+'.

Related Question