I created a directory /share
and gave chmod 2770
permission and chown root:stock /share
.
1) When I create touch a file inside /share
, I see the file has rw-rw-r--
and I don’t see rwxrws---
2) When I create a directory in /share/data
I see the permission as drwxrwsr-x
where are the parent directory is drwxrws---
How can I get parent child files and child directories to inherent parent permissions exactly the same.
Advertisement
Answer
When you create a file or directory
The owner of the new file or directory will be your effective user id (
euid
). You can change user id beforehand with thesu other_user
command (which will prompt you for the password ofother_user
), orsudo su other_user
(which will allow you or not, possibly asking for your password, according to the settings in/etc/sudoers*
). After creating the file or directory, you can change its owner withsudo chown other_user file_name
.The group of the new file or directory will be your effective group id. You can change your group id with the
newgrp other_group
command beforehand. If your current directory hasother_group
as group and itssetgid
bit is set, your effective group id will beother_group
. After creating the file or directory, you can change its group withchgrp other_group file_name
.newgrp
,chgrp
andsetgid
will work if you are a member ofother_group
. If you are not, they won’t: a group password mechanism is theoretically still in place, but it was deprecated decades ago and I’ve never seen anybody using it. Of course, you can alwayssudo chgrp other_group file_name
, or evensudo chown other_user:other_group file_name
if you want to change both.The read and write permissions of the new file or directory will depend on your
umask
, which is normally set by your configuration files at login. The most used umask values are022
which, for files, will give you-rw-r--r--
and002
which will give you-rw-rw-r--
. The commandumask
will give you your current value. You can set another value withumask new_value
and it will be effective till you change it or exit your shell. Directories will have also all execution permissions set by default, unless you have odd values inumask
, which will block the corresponding execution bit. E.g. a umask value of027
will create files with-rw-r-----
and directories withdrwxrwx---
. Please refer to documentation for a complete explanation. Also, if the parent directory has thesetgid
bit, the new directory will have it too. There is no way of setting thesetuid
andsticky
bits by default, nor thesetgid
bit for files.After the fact, you can always set the permissions you want with the command
chmod
.
That said, there is no standard command which will do what you want. However, you can easily write bash functions like the following and use them (write them in a file mycreat_functions
and source mycreat_functions
when needed). This will do for manually created files and directories. For file created by programs, shell redirections and the like, you will still have to correct the permissions manually.
function mymkdir () { local parentperms for a in "$@"; do mkdir "$a" # This copies all permissions of the parent, # exactly as they are parentperms="$(stat -c%a $(dirname "$a"))" chmod "$parentperms" "$a" # if I’m root... if [ $(id -u) = 0 ]; then chown "$(stat -c%u:%g "$a")" "$a" fi done } function mytouch () { local parentperms newperms for a in "$@"; do touch "$a" # This inherits all permissions of the parent, # but removes the excution and setgid bits, as is # appropriate for files. parentperms="$(stat -c%a $(dirname "$a"))" newperms="$(printf %o $((8#$parentperms & 8#5666)))" chmod "$newperms" "$a" # if I’m root... if [ $(id -u) = 0 ]; then chown "$(stat -c%u:%g "$a")" "$a" fi done }
Note: Owner, group and permissions are stored in an inode, where there is also other information on how to retrieve the file contents; the directory entry associates the inode with the file name, and ls -i
shows the inode numbers of the listed files. When you copy a file, you create a new directory entry and allocate a new inode, so everything mentioned here applies. When you move a file, you create a new directory entry in the new location, but have it point to the old inode, so that owner, group and permissions are effectively untouched. If you want them to change according to the new directory entry’s parent, you have to create a mymv
function along the lines of mytouch
and mymkdir
above.