Bash Scripting - 如何设置创建新文件的组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1321168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash Scripting - How to set the group that new files will be created with?
提问by Rory
I'm doing a bash shell script and I want to change the default group that new files are created as. I know you use umask
to change the permissions. Is there something for the group?
我正在做一个 bash shell 脚本,我想更改创建新文件的默认组。我知道你umask
用来更改权限。有什么适合团体的吗?
采纳答案by moonshadow
回答by mark4o
There are a couple ways to do this:
有几种方法可以做到这一点:
You can change the default group for all files created in a particular directory by setting the setgid flag on the directory (
chmod g+s _dir_
). New files in the directory will then be created with the group of the directory (set usingchgrp <group> <dir>
). This applies to any program that creates files in the directory.Note that this is automagically inherited for new subdirectories (as of Linux 3.10), however, if sub-directories were already present, this change won't be applied to them (use the
-R
flag for that).If the setgid flag is not set, then the default group will be set to the current group id of the creating process. Although this can be set using the
newgrp
command, that creates a new shell that is difficult to use within a shell script. If you want to execute a particular command (or set of commands) with the changed group, use the commandsg <group> <command>
.sg
is not a POSIX standard command but is available on Linux.
您可以通过在目录 (
chmod g+s _dir_
)上设置 setgid 标志来更改在特定目录中创建的所有文件的默认组。然后将使用目录组(使用 设置chgrp <group> <dir>
)创建目录中的新文件。这适用于在目录中创建文件的任何程序。请注意,对于新的子目录(从 Linux 3.10 开始),这是自动继承的,但是,如果子目录已经存在,则此更改将不会应用于它们(
-R
为此使用标志)。如果未设置 setgid 标志,则默认组将设置为创建进程的当前组 ID。虽然这可以使用
newgrp
命令来设置,但这会创建一个很难在 shell 脚本中使用的新 shell。如果要使用更改的组执行特定命令(或命令集),请使用命令sg <group> <command>
。sg
不是 POSIX 标准命令,但在 Linux 上可用。