Bash script that creates a directory structure

I struggled to find a way to create a script that creates a directory structure. It looks something like this:

 /
 shared
 shared / projects
 shared / series
 shared / movies
 shared / movies / action

You understand.

The file that the script reads is as follows:

 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 a file and runs the following for each line: If the directory exists, it is placed in the directory and creates a structure from there, if the Directory does not exist, create it.
When all entries in the line are preceding, return to the original directory and read the next line.

My system is Ubuntu 10.10.

So far, Ive been doing this, but that doesn't work.

#!/bin/bash pwd=$(pwd) for structure in ${column[*]} do if [ $structure ] then cd $structure else mkdir $structure fi done cd $pwd 
+8
linux bash if-statement
source share
6 answers

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 a script that runs mkdir -p $dir , where $dir is the folder folder of your directory tree.

+24
source share

If struct.txt contains the directory structure you mentioned, just run:

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

sed will delete the empty 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 a directory and the -p flag will create any parent directories if necessary.

+7
source share

mkdir has the -p flag, which creates all the parent directories of the directory you create, if necessary. you can just read each line, turn it into a path (i.e. s/ /\//g ) and call mkdir -p $path in each line

+5
source share

1) Do something like this

 find . -type d > folder_list.txt 

to create a list of folders that you need to create.

2) Move the list to the destination

3) Restore the structure in a new place:

 cat folder_list.txt | xargs mkdir 

note that in this case you do not need the -p option, although this will not hurt either.

+3
source share

I use this script in my .bash_profile, which I use for new projects:

 alias project_setup="mkdir Sites Documents Applications Website_Graphics Mockups Logos Colors Requirements Wireframes" 

If you want to create a subfolder structure, you can do something like:

 alias shared_setup="mkdir Shared shared/projects shared/series shared/movies shared/movies/action" 
+1
source share

Assuming you want to create a folder / directory tree as shown below:

  tmpdir ________|______ | | | branches tags trunk | sources ____|_____ | | includes docs 

It is also assumed that you have a variable that mentions directory names.
DOMAIN_NAME=includes,docs

You can run the command below:
$ eval "mkdir -p tmpdir/{trunk/sources/{${DOMAIN_NAME}},branches,tags}"

Note. Use a version of BASH that supports braces.

0
source share

All Articles