Linux 创建目录结构的 Bash 脚本

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

Bash script that creates a directory structure

linuxbashif-statement

提问by user668905

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

回答by B_.

mkdir has a flag -pthat 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 $pathon each line

mkdir 有一个标志-p,可以根据需要创建您正在创建的目录的所有父目录。您只需读取每一行,将其转换为路径(即s/ /\//g)并mkdir -p $path在每一行上调用

回答by littleadv

You can use mkdir -p shared/projects/movies/actionto 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/moviesshared/projects/movies/action

So basically you need script that runs mkdir -p $dirwhere $diris the leaf directory of your directory tree.

所以基本上你需要的脚本,运行mkdir -p $dir其中$dir的目录树的叶子目录。

回答by Lawrence Woodman

If struct.txtcontains the directory structure that you mention, then just run:

如果struct.txt包含您提到的目录结构,则只需运行:

sed '/^$/d;s/ /\//g' struct.txt | xargs mkdir -p

sedwill remove blank lines and make the remaining lines look like directory paths.
xargswill take each line and pass it as a parameter to mkdir.
mkdirwill make the directory and the -pflag will create any parent directories if needed.

sed将删除空行并使剩余的行看起来像目录路径。
xargs将获取每一行并将其作为参数传递给mkdir.
mkdir-p创建目录,如果需要,标志将创建任何父目录。

回答by Brett

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"

回答by witkacy26

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

回答by AjayKumarBasuthkar

Assuming you wish to create a tree of folders / directories as below:

假设您希望创建一个文件夹/目录树,如下所示:

          tmpdir
    ________|______ 
   |        |      |
branches   tags  trunk
                   |
                 sources
               ____|_____
              |          |
          includes     docs
          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=includes,docs

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 版本。

回答by Matthias Altmann

For my solution it was important to me:

对于我的解决方案,对我来说很重要:

a) I wanted to be able to edit the directory structure directly in my bash script so that I didn't have to jump back and forth between two files

a) 我希望能够直接在我的 bash 脚本中编辑目录结构,这样我就不必在两个文件之间来回跳转

b) The code for the folders should be as clear as possible without redundancy with the same paths, so that I can change it easily

b) 文件夹的代码应该尽可能清晰,路径相同,没有冗余,以便我可以轻松更改

# Creates the folder structure defined in folder structure section below
function createFolderStructure() {
     depth="1"
     while (( "$#" )); do
         while ((  != $depth )); do
             cd ..
             (( depth-- ))
         done
         shift
         mkdir ""
         cd ""
         (( depth++ ))
         shift
       done
     while (( 1 != $depth )); do
         cd ..
         (( depth-- ))
     done
}

# Folder Structure Section
read -r -d '' FOLDERSTRUCTURE << EOM
1 shared
     2 projects 
          3 movies
                4 action
     2 series
     2 backup
EOM

createFolderStructure $FOLDERSTRUCTURE

Git needs files to record directories. So I put a readme file in each directory and extended the script as follows:

Git 需要文件来记录目录。所以我在每个目录下放了一个自述文件,对脚本进行了如下扩展:

# Creates the folder structure defined in folder structure section below
function createFolderStructure() {
     depth="1"
     while (( "$#" )); do
         while ((  != $depth )); do
             cd ..
             (( depth-- ))
         done
         shift
         mkdir ""
         cd ""
         (( depth++ ))
         shift
         shift
         out=""
         while [[ "" != "-" ]]; do
             out=$out" """
             shift
         done
         shift
         echo "$out" > README.md
     done
     while (( 1 != $depth )); do
         cd ..
         (( depth-- ))
     done
}

# If you like you can read in user defined values here and use them as variables in the folder structure section, e.g.
# echo -n "Enter month of films"
# read month
# ...
# 1 shared - Folder for shared stuff -
#    2 $month - Films from month $month - 
#       3 projects - Folder for projects -
# ... 

# Folder Structure Section
read -r -d '' FOLDERSTRUCTURE << EOM
1 shared - Folder for shared stuff -
     2 projects - Folder for projects -
          3 movies - Folder for movies -
                4 action - Folder for action movies -
     2 series - Folder for series -
     2 backup - Backup folder -
EOM

createFolderStructure $FOLDERSTRUCTURE