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
1) Do something like this
to create a list of the folders you need to create.
2) Transfer the list to your destination
3) Recreate the structure in your new location:
notice that you don't need '-p' option in this case though it wouldn't hurt too.
I use this script in my .bash_profile that I use for new projects:
If you want to make a nested folder structure you you could do something like:
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 callmkdir -p $path
on each lineFor 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
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
Git needs files to record directories. So I put a readme file in each directory and extended the script as follows:
You can use
mkdir -p shared/projects/movies/action
to create the whole tree: it will createshared
, thenshared/projects
, thenshared/projects/movies
, andshared/projects/movies/action
.So basically you need script that runs
mkdir -p $dir
where$dir
is the leaf directory of your directory tree.Assuming you wish to create a tree of folders / directories as below:
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.