Linux 是否可以使用shell脚本递归创建文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8274920/
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
Is it possible to recursively create folders using shell script?
提问by ulima69
Im trying to create recursively level of directories like
/folder1/folder2/folder3
我试图创建递归级别的目录,例如
/folder1/folder2/folder3
Im trying this mkdir folder1/folder2/folder3
, but doesnt work.
how can I do it?
我正在尝试这个mkdir folder1/folder2/folder3
,但不起作用。我该怎么做?
thanks
谢谢
采纳答案by 0xd
You should pass the -p
parameter to mkdir
so it will create all the subfolders.
So following your example:
您应该将-p
参数传递给mkdir
它,以便它创建所有子文件夹。所以按照你的例子:
mkdir -p folder1/folder2/folder3
回答by wirdatrd
You may also want to add -Z to make sure the security context is that of the current directory while you're at it:
您可能还想添加 -Z 以确保安全上下文是当前目录的安全上下文:
mkdir -p -Z dir1/dir2/dir3/dir4
mkdir -p -Z dir1/dir2/dir3/dir4
assuming that's what you want and you want to avoid having to do a bunch of chown'ing and chmod'ing later.
假设这就是你想要的,并且你想避免以后必须做一堆 chown'ing 和 chmod'ing。