如何通过单个命令创建目录并授予权限

发布于 2024-11-03 08:20:31 字数 205 浏览 5 评论 0原文

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

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

命令

mkdir path/foldername
chmod 777 path/foldername 

我不喜欢在两个命令中创建和授予权限的 。我可以用单个命令执行此操作吗?

How to create a directory and give permission in single command in 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

遥远的她 2024-11-10 08:20:31

根据 mkdir 的手册页...

mkdir -m 777 dirname

According to mkdir's man page...

mkdir -m 777 dirname
真心难拥有 2024-11-10 08:20:31

当目录已存在时:

mkdir -m 777 /path/to/your/dir

当目录不存在且要创建父目录时:

mkdir -m 777 -p /parent/dirs/to/create/your/dir

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
听不够的曲调 2024-11-10 08:20:31
install -d -m 0777 /your/dir

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

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.

神经大条 2024-11-10 08:20:31

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

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

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

install -d  -g systemd-journal -m 2755 -v /var/log/journal
沙沙粒小 2024-11-10 08:20:31

只是为了扩展和改进上面的一些答案:

首先,我将检查 GNU Coreutils 8.26 的 mkdir 手册页——它为我们提供了有关选项“-m”和“-p”的信息(也可以给出分别为 --mode=MODE 和 --parents):

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

...如果存在则没有错误,根据需要创建父目录

在我看来,这些语句含糊不清。但基本上,它表示您可以使用“chmod 数字符号”(八进制)指定的权限创建目录,或者您可以采用“另一种方式”并使用/您的 umask。

旁注:我说“另一种方式”,因为 umask 值实际上正是它听起来的样子 - 掩码,隐藏/删除权限,而不是像 chmod 的数字八进制表示法那样“授予”权限。

您可以执行shell内置命令umask来查看您的3位umask是什么;对我来说,它是022。这意味着当我在给定文件夹(例如,mahome)中执行 mkdir yodirectorystat 时,我将得到一些类似于以下内容的输出:

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

现在,添加一个关于这些八进制权限的更多信息。当您创建目录时,“您的系统”会采用您的默认目录权限[适用于目录(其值应为 777)] 并使用 yo(u)mask,有效隐藏其中的一些目录烫发'。我的 umask 是 022——现在,如果我们从 777 中“减去”022(从技术上讲,减去是一种过于简单化的做法,而且并不总是正确的——我们实际上是在关闭 Perms 或屏蔽它们)...我们得到 755如前所述(或“陈述”)。

我们可以省略 3 位八进制前面的“0”(因此它们不必是 4 位数字),因为在我们的例子中,我们不需要(或者更确切地说没有提及)任何粘性位、setuid 或setgids(你可能想研究一下这些,顺便说一句,它们可能很有用,因为你要去 777)。换句话说,0777 意味着(或等于)777(但 777 不一定等于 0777 - 因为 777 只指定权限,而不指定 setuid、setgids 等)

现在,将其应用于您的问题从更广泛的意义上来说——你(已经)有一些选择。上面的所有答案都有效(至少根据我的 coreutils)。但是,当您想要一次性创建具有 777 权限的目录(嵌套目录)时,您可能(或很可能)会遇到上述解决方案的问题。具体来说,如果我在 mahome 中使用 umask 022 执行以下操作:

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

我将获得 yodirectoryyostuff 的权限 755,仅使用 < code>777 权限用于 mastuffinyostuff。所以看起来 umask 就是 yodirectoryyostuff 上的全部内容...为了解决这个问题,我们可以使用一个子 shell:

( umask 000 && mkdir -p yodirectory/yostuff/mastuffinyostuff )

就是这样。 777 yostuff、mastuffinyostuff 和 yodirectory 的权限。

Just to expand on and improve some of 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):

...set[s] file mode (as in chmod), not 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.

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.

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:

               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 oversimplification and not always correct - we are actually turning off perms or masking them)...we get 755 as stated (or "statted") earlier.

We can omit the '0' in front of the 3-digit octal (so they don't have to be 4 digits) since in our case we didn't want (or rather didn't mention) any sticky bits, 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.)

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:

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:

( umask 000 && mkdir -p yodirectory/yostuff/mastuffinyostuff )

and that's it. 777 perms for yostuff, mastuffinyostuff, and yodirectory.

半山落雨半山空 2024-11-10 08:20:31

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

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

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

./scriptname path/foldername

但是, alex 的答案 好得多,因为它生成一个进程而不是三个。我不知道 -m 选项。

You could write a simple shell script, for example:

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

Once saved, and the executable flag enabled, you could run it instead of mkdir and 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.

枫林﹌晚霞¤ 2024-11-10 08:20:31

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

mkdir -m777 path/foldername 

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

mkdir -m777 path/foldername 
对风讲故事 2024-11-10 08:20:31

不要这样做: mkdir -m 777 -pa/b/c 因为这只会在最后一个目录 c 上设置权限 777; a 和 b 将使用 umask 的默认权限创建。

要创建具有权限 777 的任何新目录,请在覆盖 umask 的子 shell 中运行 mkdir -p

(umask u=rwx,g=rwx,o=rwx && mkdir -p a/b/c)

请注意,如果存在以下任何一种情况,这不会更改权限 : 、b 和 c 已经存在。

Don't do: mkdir -m 777 -p a/b/c since that will only set permission 777 on the last directory, c; a and b will be created with the default permission from your umask.

Instead to create any new directories with permission 777, run mkdir -p in a subshell where you override the umask:

(umask u=rwx,g=rwx,o=rwx && mkdir -p a/b/c)

Note that this won't change the permissions if any of a, b and c already exist though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文