创建一个目录结构bash脚本 [英] Bash script that creates a directory structure
问题描述
我一直在谷歌上搜索通宵试图找到一种方法来创建,创建一个目录结构的脚本。这看起来是这样的:
/
共享
共享/项目
共享/系列
共享/电影
共享/电影/动作
您明白了吧。
该脚本这个样子的读取文件:
共享备份
共享数据
共享项目
共享projcets系列
共享项目的电影
共享项目动作电影
我想创建一个脚本文件中读取每一行,然后对每个行:
如果该目录存在,则它把本身在目录中,并从那里建立的结构中,如果
该目录不存在,创建它。结果
当行中所有条目已被pceded $ P $,回到原来的目录并读取下一行。
我的系统是Ubuntu的10.10。
到目前为止,我已经做到了这一点,但它不工作。
#!/斌/庆典PWD = $(PWD)在$ {结构柱[*]}
做
如果[$结构]
然后
CD $结构
其他
MKDIR $结构
科幻
DONECD $ PWD
您可以使用的mkdir -p共享/项目/电影/动作
以创建整个树:它将创建共享
,那么共享/项目
,那么共享/项目/电影
和共享/项目/电影/动作
。
所以基本上你需要运行脚本的mkdir -p $ DIR
,其中 $ DIR
是叶目录中目录树。
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.
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
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
.
So basically you need script that runs mkdir -p $dir
where $dir
is the leaf directory of your directory tree.
这篇关于创建一个目录结构bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!