简体   繁体   English

用于创建目录结构的Bash脚本

[英]Bash script that creates a directory structure

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

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来创建整个树:它将创建shared ,然后shared/projects ,然后shared/projects/movies ,以及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是目录树的叶子目录。

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. sed将删除空行并使其余行看起来像目录路径。
xargs will take each line and pass it as a parameter to mkdir . xargs将获取每一行并将其作为参数传递给mkdir
mkdir will make the directory and the -p flag will create any parent directories if needed. mkdir将创建目录, -p标志将根据需要创建任何父目录。

mkdir has a flag -p that creates all the parent directories of the directory you're creating if needed. mkdir有一个标志-p ,可以根据需要创建您正在创建的目录的所有父目录。 you can just just read each line, turn it into a path (ie s/ /\\//g ) and call mkdir -p $path on each line 您可以只读取每一行,将其转换为路径(即s/ /\\//g )并在每行上调用mkdir -p $path

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'选项,尽管它也不会受到伤害。

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"

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

You may issue below command: 您可以发出以下命令:
$ eval "mkdir -p tmpdir/{trunk/sources/{${DOMAIN_NAME}},branches,tags}"

Note: use the BASH version that supports curly-braces expansion. 注意: 使用支持花括号扩展的BASH版本。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM