Question : Bash Script, Search for a given directory

I'm trying to write a script that prompts the user to type in the name of a directory that is somewhere in their home directory (IE could be a subdirectory) and then searches for that directory and backs it up using tar/gzip.

The problem I'm having is that I can't get bash to properly search for a directory, I tried using "find -d" but I could never get it to work properly with subdirectories.

Here's my current code (sorry for the mess)

 #Prompt the user to input the directory name they want backed up.
        echo "You pressed Y, Please input the name of the directory you want ba$

        #Read the directory name that the user inputs.
        read directoryName
                                                         

        #Repeats the directory name back to the user to confirm their choice.
        echo "You entered $directoryName";


        #Check if the directory exists
        if [ -d "$directoryName" ]; then


        echo "File exists, Now is processing backup to your home directory";
 
backup=$directoryName
OUTPUT=~/backups/$backup`/bin/date +"%Y_%h_%d_%T"`.tar.gz
       
#Back up the files in the fileExtension path in backup dir.
tar -czf $OUTPUT $backup
exit;

The big problem is the above code doesnt actually search through all the users directories and subdirectories for the given directory. Is there any way to do this with "find" or some other method? I'm stuck.

Answer : Bash Script, Search for a given directory

# changed : to _ and removed the -xv
# also a few minor changes

#!/bin/bash
#
today=$(/bin/date +"%Y_%h_%d_%H_%M_%S")
read -p "Please input the name of the directory you want back up : " directoryName

echo "You entered $directoryName"

if [ "$(find -type d -name "$directoryName" | wc -l)" -gt "1" ];then
      find -type d -name "$directoryName"
      read -p "more then one directories . enter the one you want to backup : " backupfile
      tar cfz $backupfile.$today.tar.gz $backupfile
else
      backupfile=$(find -type d -name "$directoryName")
            tar cfz $backupfile.$today.tar.gz $backupfile
fi
      mv $backupfile.$today.tar.gz $HOME/backups
Random Solutions  
 
programming4us programming4us