bash 正确使用 mkdir -m -p 和 chown

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25873479/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:22:02  来源:igfitidea点击:

Using mkdir -m -p and chown together correctly

linuxbashshmkdir

提问by jquery

I would like to create a directory using a bash script and then set the mode to 00755 at the same time

我想使用bash脚本创建一个目录,然后同时将模式设置为00755

mkdir -p -m=00755 "/dir/dir2"

Is this the correct way of using them together and can I also add chown command to the same line while creating them?

这是一起使用它们的正确方法吗,我还可以在创建它们时将 chown 命令添加到同一行吗?

回答by Ignacio Vazquez-Abrams

It goes a little like this:

它有点像这样:

install -d -m 0755 -o someuser -g somegroup /dir/dir2

回答by hek2mgl

If you want to set the owner during creation, you can simply impersonate as this user, using sudofor example:

如果您想在创建过程中设置所有者,您可以简单地模拟为该用户,sudo例如使用:

sudo -uTHE_USER mkdir -p -m=00755 "/dir/dir2"

This has the advantage that there will be no time difference between creation and changing the ownership, which could otherwise being harmful if exploited.

这样做的优点是创建和更改所有权之间没有时间差,否则如果被利用可能会有害。

回答by phil

Yes that should work. As for the chown, simply follow the command ' && chown... '. && is similar to ; except the next command ONLYexecutes if the previous command exits success (0).

是的,这应该有效。至于chown,只需按照命令' && chown... '。&& 类似于 ; 除了下一个命令在上一个命令退出成功 (0)时才执行。