I've been googling all night trying to find a way to create a script that creates a directory structure. That looks something like this:
我一直在谷歌搜索,试图找到一种方法来创建一个创建目录结构的脚本。看起来像这样:
/ shared shared/projects shared/series shared/movies shared/movies/action
You get the point.
你明白了。
The file that the script reads from look like this:
脚本从中读取的文件如下所示:
shared backup shared data shared projects shared projcets series shared projects movies shared projects movies action
I want to create a script that reads each line in the file and run the following for each line: If the directory exist, it places itself in the directory and create the structure from there, if The directory doesn’t exist, create it.
When all entries in the row have been preceded by, go back to original directory and read the next line.
我想创建一个脚本来读取文件中的每一行,并为每一行运行以下内容:如果目录存在,它将自己放在目录中并从那里创建结构,如果该目录不存在,则创建它。当行中的所有条目都在前面时,返回原始目录并阅读下一行。
My system is Ubuntu 10.10.
我的系统是Ubuntu 10.10。
So far I’ve done this, but it doesn’t work.
到目前为止,我已经做到了这一点,但它不起作用。
#!/bin/bash
pwd=$(pwd)
for structure in ${column[*]}
do
if [ $structure ]
then
cd $structure
else
mkdir $structure
fi
done
cd $pwd
6 个解决方案
#1
24
You can use mkdir -p shared/projects/movies/action
to create the whole tree: it will create shared
, then shared/projects
, then shared/projects/movies
, and shared/projects/movies/action
.
您可以使用mkdir -p shared / projects / movies / action来创建整个树:它将创建共享,然后共享/项目,然后共享/项目/电影,以及共享/项目/电影/动作。
So basically you need script that runs mkdir -p $dir
where $dir
is the leaf directory of your directory tree.
所以基本上你需要运行mkdir -p $ dir的脚本,其中$ dir是目录树的叶子目录。
#2
7
If struct.txt
contains the directory structure that you mention, then just run:
如果struct.txt包含您提到的目录结构,那么只需运行:
sed '/^$/d;s/ /\//g' struct.txt | xargs mkdir -p
sed
will remove blank lines and make the remaining lines look like directory paths.xargs
will take each line and pass it as a parameter to mkdir
.mkdir
will make the directory and the -p
flag will create any parent directories if needed.
sed将删除空行并使其余行看起来像目录路径。 xargs将获取每一行并将其作为参数传递给mkdir。 mkdir将创建目录,如果需要,-p标志将创建任何父目录。
#3
5
mkdir has a flag -p
that creates all the parent directories of the directory you're creating if needed. you can just just read each line, turn it into a path (i.e. s/ /\//g
) and call mkdir -p $path
on each line
mkdir有一个标志-p,它根据需要创建您正在创建的目录的所有父目录。您只需读取每一行,将其转换为路径(即s / / \ _g)并在每行上调用mkdir -p $ path
#4
3
1) Do something like this
1)做这样的事情
find . -type d > folder_list.txt
to create a list of the folders you need to create.
创建需要创建的文件夹列表。
2) Transfer the list to your destination
2)将列表转移到目的地
3) Recreate the structure in your new location:
3)在新位置重新构建结构:
cat folder_list.txt | xargs mkdir
notice that you don't need '-p' option in this case though it wouldn't hurt too.
请注意,在这种情况下你不需要'-p'选项,尽管它也不会受到伤害。
#5
1
I use this script in my .bash_profile that I use for new projects:
我在我用于新项目的.bash_profile中使用此脚本:
alias project_setup="mkdir Sites Documents Applications Website_Graphics Mockups Logos Colors Requirements Wireframes"
If you want to make a nested folder structure you you could do something like:
如果要创建嵌套文件夹结构,可以执行以下操作:
alias shared_setup="mkdir Shared shared/projects shared/series shared/movies shared/movies/action"
#6
0
Assuming you wish to create a tree of folders / directories as below:
假设您希望创建一个文件夹/目录树,如下所示:
tmpdir ________|______ | | | branches tags trunk | sources ____|_____ | | includes docs
Also assuming that you have a variable that mentions the directory names.DOMAIN_NAME=includes,docs
还假设您有一个提及目录名称的变量。 DOMAIN_NAME =包括,文档
You may issue below command:$ eval "mkdir -p tmpdir/{trunk/sources/{${DOMAIN_NAME}},branches,tags}"
您可以发出以下命令:$ eval“mkdir -p tmpdir / {trunk / sources / {$ {DOMAIN_NAME}},branches,tags}”
Note: use the BASH version that supports curly-braces expansion.
注意:使用支持花括号扩展的BASH版本。
#1
24
You can use mkdir -p shared/projects/movies/action
to create the whole tree: it will create shared
, then shared/projects
, then shared/projects/movies
, and shared/projects/movies/action
.
您可以使用mkdir -p shared / projects / movies / action来创建整个树:它将创建共享,然后共享/项目,然后共享/项目/电影,以及共享/项目/电影/动作。
So basically you need script that runs mkdir -p $dir
where $dir
is the leaf directory of your directory tree.
所以基本上你需要运行mkdir -p $ dir的脚本,其中$ dir是目录树的叶子目录。
#2
7
If struct.txt
contains the directory structure that you mention, then just run:
如果struct.txt包含您提到的目录结构,那么只需运行:
sed '/^$/d;s/ /\//g' struct.txt | xargs mkdir -p
sed
will remove blank lines and make the remaining lines look like directory paths.xargs
will take each line and pass it as a parameter to mkdir
.mkdir
will make the directory and the -p
flag will create any parent directories if needed.
sed将删除空行并使其余行看起来像目录路径。 xargs将获取每一行并将其作为参数传递给mkdir。 mkdir将创建目录,如果需要,-p标志将创建任何父目录。
#3
5
mkdir has a flag -p
that creates all the parent directories of the directory you're creating if needed. you can just just read each line, turn it into a path (i.e. s/ /\//g
) and call mkdir -p $path
on each line
mkdir有一个标志-p,它根据需要创建您正在创建的目录的所有父目录。您只需读取每一行,将其转换为路径(即s / / \ _g)并在每行上调用mkdir -p $ path
#4
3
1) Do something like this
1)做这样的事情
find . -type d > folder_list.txt
to create a list of the folders you need to create.
创建需要创建的文件夹列表。
2) Transfer the list to your destination
2)将列表转移到目的地
3) Recreate the structure in your new location:
3)在新位置重新构建结构:
cat folder_list.txt | xargs mkdir
notice that you don't need '-p' option in this case though it wouldn't hurt too.
请注意,在这种情况下你不需要'-p'选项,尽管它也不会受到伤害。
#5
1
I use this script in my .bash_profile that I use for new projects:
我在我用于新项目的.bash_profile中使用此脚本:
alias project_setup="mkdir Sites Documents Applications Website_Graphics Mockups Logos Colors Requirements Wireframes"
If you want to make a nested folder structure you you could do something like:
如果要创建嵌套文件夹结构,可以执行以下操作:
alias shared_setup="mkdir Shared shared/projects shared/series shared/movies shared/movies/action"
#6
0
Assuming you wish to create a tree of folders / directories as below:
假设您希望创建一个文件夹/目录树,如下所示:
tmpdir ________|______ | | | branches tags trunk | sources ____|_____ | | includes docs
Also assuming that you have a variable that mentions the directory names.DOMAIN_NAME=includes,docs
还假设您有一个提及目录名称的变量。 DOMAIN_NAME =包括,文档
You may issue below command:$ eval "mkdir -p tmpdir/{trunk/sources/{${DOMAIN_NAME}},branches,tags}"
您可以发出以下命令:$ eval“mkdir -p tmpdir / {trunk / sources / {$ {DOMAIN_NAME}},branches,tags}”
Note: use the BASH version that supports curly-braces expansion.
注意:使用支持花括号扩展的BASH版本。