Skip to content
Advertisement

Bash script to copy the directory structure from source directory into target directory

I am very new to Bash scritping and to get some practice, I am attempting to write a script that takes in a source directory and a destination directory. The script will search the source directory and copy its structure of sub-directories into the target directory (any files will be ignored, just the directories themselves will be duplicated). The source directory can have any number of sub-directories at any depth. What would be the best way to achieve this? I have started by attempting to write a recursive function where, if a directory is found, the function is called recursively onto this directory. However, due to my lack of experience with scripting, I have been stumped.

Here is what I have so far:

#! /bin/bash

if [ ! $# -eq 2 ]; then 
    echo "ERROR: Script needs 2 arguments: $0 source/directory/ target/directory/"
    exit
fi

SOURCE_DIR=$1
TARGET_DIR=$2

function searchForDirectory {
    for FILE in ls $SOURCE_DIR/*; do #should be 'ls *current_directory*'
        if [ -d $FILE ]; then
            searchForDirectory #call function recursively onto $FILE
            #Do stuff here to create copy of this directory in target dir
        fi
    done
}

if [ ! -d $SOURCE_DIR ]; then 
    echo "ERROR: Source directory does not exist"
    exit
else
    searchForDirectory
fi

I know that this is just a skeleton function, and a lot more work would need to be put into it, but I’m just looking for guidance as to whether this is the correct way to go about this before I go any further, and if so, what would be my next step? How do I pass a directory into my function?

EDIT: Here is my solution thanks to Ivan’s help below #! /bin/bash

if [ ! $# -eq 2 ]; then 
    echo -e "nERROR: Script needs 2 arguments:n$0 source/directory/ target/directory/n"
    exit
fi

function recursiveDuplication {
    for file in `ls $1`; do
        if [ -d $1/$file ] && [ ! -d $2/$file ]; then
            mkdir $2/$file
            recursiveDuplication $1/$file $2/$file
        elif [[ $1/$file == *.png ]]; then
            convert $1/$file $2/$file
        fi
    done
}

if [ ! -d $1 ]; then 
    echo -e "nERROR: $1 does not existn"
    exit
elif [ -d $2 ] && [ ! -w $2 ]; then
    echo -e "nERROR: You do not have permission to write to $2n"
    exit
elif [ ! -d $2 ]; then
    echo -e "nSUCCESS: $2 has been created"
    mkdir $2
fi

recursiveDuplication $1 $2

There are two issues with this solution:

  1. As Rany Albeg Wein explained below, using ‘ls’ is not a good solution – and I have seen why when there is a space in a directory/*.png name.

  2. I am also attempting to copy any *.png file from the source to the target and convert it to a *.jpg image in the target. How can I do this? I am attempting to use ImageMagick’s convert image.png image.jpg command, but do not know how to do so when the image.png is being referred to as $file?

Advertisement

Answer

#!/bin/bash
# 1st argument - source dir, 2nd - destination

function rrr {
  for i in `ls $1`
  do
    if [ -d $1/$i ]
    then
      mkdir $2/$i
      rrr $1/$i $2/$i
    fi
  done
}


rrr $1  $2
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement