如何在单个命令中创建目录并授予权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5786326/
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
How to create a directory and give permission in single command
提问by poorani
How to create a directory and give permission in single command in Linux?
如何在 Linux 中创建目录并在单个命令中授予权限?
I have to create lots of folder with full permission 777
.
我必须创建很多具有完全权限的文件夹777
。
Commands
命令
mkdir path/foldername
chmod 777 path/foldername
I don't like to create and give permission in two commands. Can I do this in single command?
我不喜欢在两个命令中创建和授予权限。我可以在单个命令中执行此操作吗?
回答by Delan Azabani
You could write a simple shell script, for example:
您可以编写一个简单的 shell 脚本,例如:
#!/bin/bash
mkdir ""
chmod 777 ""
Once saved, and the executable flag enabled, you could run it instead of mkdir and chmod:
保存并启用可执行标志后,您可以运行它而不是 mkdir 和 chmod:
./scriptname path/foldername
However, alex's answeris much better because it spawns one process instead of three. I didn't know about the -m
option.
但是,alex 的答案要好得多,因为它生成了一个进程而不是三个进程。我不知道这个-m
选项。
回答by Markus W Mahlberg
install -d -m 0777 /your/dir
should give you what you want. Be aware that every user has the right to write add and delete files in that directory.
应该给你你想要的。请注意,每个用户都有权在该目录中写入添加和删除文件。
回答by YenForYang
Just to expand on and improve some the above answers:
只是为了扩展和改进上面的一些答案:
First, I'll check the mkdir man page for GNU Coreutils 8.26 -- it gives us this information about the option '-m' and '-p' (can also be given as --mode=MODE and --parents, respectively):
首先,我将检查 GNU Coreutils 8.26 的 mkdir 手册页——它为我们提供了有关选项“-m”和“-p”的信息(也可以分别作为 --mode=MODE 和 --parents 给出):
...set[s] file mode (as in chmod), not a=rwx - umask
...no error if existing, make parent directories as needed
...设置[s] 文件模式(如在 chmod 中),而不是 a=rwx - umask
...如果存在没有错误,根据需要创建父目录
The statements are vague and unclear in my opinion. But basically, it says that you can make the directory with permissions specified by "chmod numeric notation" (octals) or you can go "the other way" and use a/your umask.
在我看来,这些陈述含糊不清。但基本上,它说您可以使用由“chmod 数字表示法”(八进制)指定的权限创建目录,或者您可以“另一种方式”并使用您的 umask。
Side note: I say "the other way" since the umask value is actually exactly what it sounds like -- a mask, hiding/removing permissions rather than "granting" them as with chmod's numeric octal notation.
旁注:我说“另一种方式”,因为 umask 值实际上正是它听起来的样子——一个掩码,隐藏/删除权限,而不是像 chmod 的数字八进制表示法那样“授予”它们。
You can execute the shell-builtin command umask
to see what your 3-digit umask is; for me, it's 022
. This means that when I execute mkdir yodirectory
in a given folder (say, mahome) and stat
it, I'll get some output resembling this:
你可以执行shell-builtin命令umask
查看你的3位umask是什么;对我来说,是022
。这意味着当我mkdir yodirectory
在给定的文件夹(比如 mahome)中执行stat
时,我会得到一些类似这样的输出:
755 richard:richard /mahome/yodirectory
# permissions user:group what I just made (yodirectory),
# (owner,group,others--in that order) where I made it (i.e. in mahome)
#
Now, to add just a tiny bit more about those octal permissions. When you make a directory, "your system" take your default directory perms' [which applies for newdirectories (its value should 777)] and slaps on yo(u)mask, effectively hiding some of those perms'. My umask is 022--now if we "subtract" 022 from 777 (technically subtracting is an oversimplication and not always correct - we are actually turning off perms or masking them)...we get 755 as stated (or "statted") earlier.
现在,再添加一点关于这些八进制权限的信息。当你创建一个目录时,“你的系统”采用你的默认目录 perms' [适用于新目录(它的值应该是 777)] 并在 yo(u) 掩码上拍打,有效地隐藏了其中一些 perms'。我的 umask 是 022——现在如果我们从 777 中“减去”022(从技术上讲,减去是一种过于简单化,并不总是正确的——我们实际上是关闭烫发或屏蔽它们)......我们得到 755 如上所述(或“统计”) ) 之前。
We can omit the '0' in front of the 3-digit octals (so they don't have to be 4 digit) since in our case we didn't want (or rather didn't mention) any stickybits, setuids or setgids (you might want to look into those, btw, they might be useful since you are going 777). So in other words, 0777 implies (or is equivalent to) 777 (but 777 isn't necessarily equivalent to 0777--since 777 only specifies the permissions, not the setuids, setgids, etc.)
我们可以省略 3 位八进制前面的“0”(因此它们不必是 4 位)因为在我们的例子中我们不想要(或者更确切地说没有提到)任何粘性位、setuids 或 setgids (你可能想看看那些,顺便说一句,它们可能有用,因为你要去 777)。所以换句话说,0777 暗示(或等价于)777(但 777 不一定等价于 0777——因为 777 只指定权限,而不是 setuids、setgids 等)
Now, to apply this to your question in a broader sense--you have (already) got a few options. All the answers above work (at least according to my coreutils). But you may (or are pretty likely to) run into problems with the above solutions when you want to create subdirectories (nested directories) with 777 permissions all at once. Specifically, if I do the following in mahome with a umask of 022:
现在,在更广泛的意义上将此应用于您的问题 - 您(已经)有一些选择。以上所有答案都有效(至少根据我的 coreutils)。但你可能(或相当可能)碰到上述问题的解决,当你想创建子一下子目录(嵌套目录)与777个权限。具体来说,如果我在 mahome 中使用 022 的 umask 执行以下操作:
mkdir -m 777 -p yodirectory/yostuff/mastuffinyostuff
# OR (you can swap 777 for 0777 if you so desire, outcome will be the same)
install -d -m 777 -p yodirectory/yostuff/mastuffinyostuff
I will get perms 755
for both yodirectory
and yostuff
, with only 777
perms for mastuffinyostuff
. So it appears that the umask
is all that's slapped on yodirectory
and yostuff
...to get around this we can use a subshell:
我会得到烫发755
两个yodirectory
和yostuff
,只777
烫发了mastuffinyostuff
。所以看起来这umask
就是全部,yodirectory
并且yostuff
......为了解决这个问题,我们可以使用一个子shell:
( umask 000 && mkdir -p yodirectory/yostuff/mastuffinyostuff )
( umask 000 && mkdir -p yodirectory/yostuff/mastuffinyostuff )
and that's it. 777 perms for yostuff, mastuffinyostuff, and yodirectory.
就是这样。yostuff、mastuffinyostuff 和 yodirectory 的 777 个烫发。
回答by Badi
IMO, it's better to use the install
command in such situations. I was trying to make systemd-journald
persistent across reboots.
IMO,install
在这种情况下最好使用该命令。我试图systemd-journald
在重新启动后保持持久性。
install -d -g systemd-journal -m 2755 -v /var/log/journal
回答by Omer Gafar
you can use following command to create directory and give permissions at the same time
您可以使用以下命令创建目录并同时授予权限
mkdir -m777 path/foldername
回答by Pedro Trujillo
When the directory already exist:
当目录已经存在时:
mkdir -m 777 /path/to/your/dir
When the directory does not exist and you want to create the parent directories:
当目录不存在并且您要创建父目录时:
mkdir -m 777 -p /parent/dirs/to/create/your/dir