简体   繁体   English

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

[英]How to create a directory and give permission in single command

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?我可以在单个命令中执行此操作吗?

根据mkdir的手册页...

mkdir -m 777 dirname
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.请注意,每个用户都有权在该目录中写入添加和删除文件。

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

IMO, it's better to use the install command in such situations. IMO,在这种情况下最好使用install命令。 I was trying to make systemd-journald persistent across reboots.我试图使systemd-journald在重新启动后持久化。

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

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):首先,我将检查 GNU Coreutils 8.26 的 mkdir 手册页——它为我们提供了有关选项“-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;你可以执行shell-builtin命令umask来查看你的3位umask是什么; for me, it's 022 .对我来说,它是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:这意味着当我在给定的文件夹(比如 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)
 # 

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'.当你创建一个目录时,“你的系统”采用你的默认目录 perms' [适用于目录(它的值应该是 777)] 并在 yo(u) 掩码上拍打,有效地隐藏了其中一些 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 mask ing them)...we get 755 as stated (or "statted") earlier.我的 umask 是 022——现在,如果我们从 777 中“减去”022(从技术上讲,减去是一种过度简化,并不总是正确的——我们实际上是关闭烫发或屏蔽它们)……我们得到 755,如所述(或“统计”) ) 之前。

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).我们可以省略 3 位八进制前面的“0”(因此它们不必是 4 位),因为在我们的例子中我们不想要(或者更确切地说没有提到)任何粘滞位、setuids 或setgids(你可能想看看那些,顺便说一句,它们可能有用,因为你要去 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.)所以换句话说,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).以上所有答案都有效(至少根据我的 coreutils)。 But you may (or are pretty likely to) run into problems with the above solutions when you want to create sub directories (nested directories) with 777 permissions all at once.但你可能(或相当可能)碰到上述问题的解决,当你想一下子就创建子目录(嵌套目录)与777个权限。 Specifically, if I do the following in mahome with a umask of 022:具体来说,如果我在 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 .我会得到烫发755两个yodirectoryyostuff ,只有777烫发的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就是yodirectoryyostuff上的yodirectory yostuff ……为了解决这个问题,我们可以使用一个子shell:

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

and that's it.就是这样。 777 perms for yostuff, mastuffinyostuff, and yodirectory. yostuff、mastuffinyostuff 和 yodirectory 的 777 个烫发。

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

mkdir -m777 path/foldername 

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.但是, alex 的答案要好得多,因为它生成了一个进程而不是三个进程。 I didn't know about the -m option.我不知道-m选项。

Don't do: mkdir -m 777 -pa/b/c since that will only set permission 777 on the last directory, c;不要这样做: mkdir -m 777 -pa/b/c因为这只会在最后一个目录 c 上设置权限777 a and b will be created with the default permission from your umask. a 和 b 将使用您的 umask 的默认权限创建。

Instead to create any new directories with permission 777 , run mkdir -p in a subshell where you override the umask:要创建具有权限777任何新目录,请在覆盖 umask 的子shell中运行mkdir -p

(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.请注意,如果 a、b 和 c 中的任何一个已经存在,这不会更改权限。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为我的Laravel应用程序授予权限在目录中创建子文件夹(Ubuntu) - How to give permission for my Laravel application create subfolders in a directory (Ubuntu) 使用linux,如何在/ dev目录之外创建一个设备文件,该文件可以授予我打开和读取该文件的权限? - Using linux, how can I create a device file outside of the /dev directory that will give me permission to open and read it? 如何复制文件并授予它们目标目录的权限 - How to copy files and give them permission of destination directory 如何授予apache从目录检索img的权限? - How do I give permission for apache to retrieve imgs from directory? 如何授予apache使用NTFS分区上的目录的权限? - How do I give apache permission to use a directory on an NTFS partition? 如何授予 apache (lamp) 在 opensuse tumbleweed 中创建目录或文件的权限? - How to give apache (lamp) permission to make directory or a file in opensuse tumbleweed? 如何在没有root权限的情况下创建目录? - How to create a directory without root permission? 在单个命令行中创建文件以及嵌套目录 - Create file along with nested directory in single command line 使用单个命令在所有目录下创建一个新目录:Linux - create a new directory under all directories using single command : Linux 如何授予在tomcat下运行的spring应用程序在另一个用户目录上写入的权限 - How to give permission to spring application running under tomcat to write on another user directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM