Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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
like image 723
user668905 Avatar asked Mar 21 '11 06:03

user668905


2 Answers

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

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

# Creates the folder structure defined in folder structure section below
function createFolderStructure() {
     depth="1"
     while (( "$#" )); do
         while (( $1 != $depth )); do
             cd ..
             (( depth-- ))
         done
         shift
         mkdir "$1"
         cd "$1"
         (( 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:

# Creates the folder structure defined in folder structure section below
function createFolderStructure() {
     depth="1"
     while (( "$#" )); do
         while (( $1 != $depth )); do
             cd ..
             (( depth-- ))
         done
         shift
         mkdir "$1"
         cd "$1"
         (( depth++ ))
         shift
         shift
         out=""
         while [[ "$1" != "-" ]]; do
             out=$out" ""$1"
             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
like image 63
secf00tprint Avatar answered Oct 16 '22 17:10

secf00tprint


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.

like image 25
littleadv Avatar answered Oct 16 '22 16:10

littleadv