如何创建一个目录并在单个命令中授予权限?

时间:2021-05-08 16:42:53

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.

我必须创建大量具有完全权限的文件夹。

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?

我不喜欢在两个命令中创建和授予权限。我可以用一个命令来做吗?

6 个解决方案

#1


136  

According to mkdir's man page...

根据mkdir的手册页…

mkdir -m 777 dirname

#2


12  

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.

应该给你想要的。请注意,每个用户都有权在该目录中编写添加和删除文件。

#3


5  

You could write a simple shell script, for example:

您可以编写一个简单的shell脚本,例如:

#!/bin/bash
mkdir "$1"
chmod 777 "$1"

Once saved, and the executable flag enabled, you could run it instead of mkdir and chmod:

一旦保存,并启用可执行标志,您可以运行它而不是mkdir和chmod:

./scriptname path/foldername

However, alex's answer is much better because it spawns one process instead of three. I didn't know about the -m option.

然而,alex的回答要好得多,因为它只产生一个过程而不是三个。我不知道-m选项。

#4


3  

IMO, it's better to use the install command in such situations. I was trying to make systemd-journald persistent across reboots.

在这种情况下,最好使用install命令。我试图让system -journald在重新启动时保持不变。

install -d  -g systemd-journal -m 2755 -v /var/log/journal

#5


2  

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):

首先,我将检查mkdir gnucoreutils 8.26的man页面——它为我们提供了关于选项'-m'和'-p'的信息(还可以分别为-mode= mode和-parents):

...set[s] file mode (as in chmod), not a=rwx - umask

…设置[s]文件模式(如chmod),而不是a=rwx - umask

...no error if existing, make parent directories as needed

…如果存在,请根据需要创建父目录

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。这意味着当我在给定的文件夹(比如mahome)中执行mkdir yodirectory并对其进行统计时,我将得到类似如下的输出:

               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 new directories (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(技术上的减法是一种过于简单的方法,并不总是正确的——我们实际上是关闭了perms或屏蔽了它们)……我们前面已经提到了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.)

我们可以省略前面的‘0’三个八进制(因此他们不必4位数)以来,在我们的例子中我们不希望任何stickybits(或者说没有提到),setuid和setgid(你可能想看看,顺便说一句,他们可能是有用的,因为你是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:

我要为yodirectory和yostuff做755号perms, mastuffinyostuff做777号perms。所以看起来umask就是给yodirectory和yostuff的所有东西……为了解决这个问题,我们可以使用一个子层:

( 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 perms。

#6


0  

you can use following command to create directory and give permissions at the same time

您可以使用以下命令创建目录并同时授予权限

mkdir -m777 path/foldername 

#1


136  

According to mkdir's man page...

根据mkdir的手册页…

mkdir -m 777 dirname

#2


12  

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.

应该给你想要的。请注意,每个用户都有权在该目录中编写添加和删除文件。

#3


5  

You could write a simple shell script, for example:

您可以编写一个简单的shell脚本,例如:

#!/bin/bash
mkdir "$1"
chmod 777 "$1"

Once saved, and the executable flag enabled, you could run it instead of mkdir and chmod:

一旦保存,并启用可执行标志,您可以运行它而不是mkdir和chmod:

./scriptname path/foldername

However, alex's answer is much better because it spawns one process instead of three. I didn't know about the -m option.

然而,alex的回答要好得多,因为它只产生一个过程而不是三个。我不知道-m选项。

#4


3  

IMO, it's better to use the install command in such situations. I was trying to make systemd-journald persistent across reboots.

在这种情况下,最好使用install命令。我试图让system -journald在重新启动时保持不变。

install -d  -g systemd-journal -m 2755 -v /var/log/journal

#5


2  

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):

首先,我将检查mkdir gnucoreutils 8.26的man页面——它为我们提供了关于选项'-m'和'-p'的信息(还可以分别为-mode= mode和-parents):

...set[s] file mode (as in chmod), not a=rwx - umask

…设置[s]文件模式(如chmod),而不是a=rwx - umask

...no error if existing, make parent directories as needed

…如果存在,请根据需要创建父目录

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。这意味着当我在给定的文件夹(比如mahome)中执行mkdir yodirectory并对其进行统计时,我将得到类似如下的输出:

               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 new directories (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(技术上的减法是一种过于简单的方法,并不总是正确的——我们实际上是关闭了perms或屏蔽了它们)……我们前面已经提到了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.)

我们可以省略前面的‘0’三个八进制(因此他们不必4位数)以来,在我们的例子中我们不希望任何stickybits(或者说没有提到),setuid和setgid(你可能想看看,顺便说一句,他们可能是有用的,因为你是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:

我要为yodirectory和yostuff做755号perms, mastuffinyostuff做777号perms。所以看起来umask就是给yodirectory和yostuff的所有东西……为了解决这个问题,我们可以使用一个子层:

( 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 perms。

#6


0  

you can use following command to create directory and give permissions at the same time

您可以使用以下命令创建目录并同时授予权限

mkdir -m777 path/foldername