Link to home
Start Free TrialLog in
Avatar of Polaris2k9
Polaris2k9

asked on

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.
Avatar of 0ren
0ren
Flag of Israel image

you can look for a directory using find like htis:
find -type d -name dirname

what about a directory name that exist twice ?
the command

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

will only work if you entered the full directory path
if so you dont need to look for it
#thats the first part

read -p "You pressed Y, Please input the name of the directory you want back up : " directoryName
echo "You entered $directoryName"

#if you want to look for the dir name then

find -type d -name "directoryName"
all together now

#!/bin/bash

read -p "You pressed Y, Please input the name of the directory you want back up : " directoryName
echo "You entered $directoryName"

backupname=$(find -type d -name "$directoryName")
date=$(/bin/date +"%Y_%h_%d_%T")
tar cvfz $backupname.$date.tar.gz $backupname

note it will work only if the directory has a unique name
if it will not be unique then a for loop should be introduced
let me know
and remove the v from the tar command to make it faster ( v for verbose )
tar cfz
#if there are few dirs with the same name:
# note it will create the tar in the same directory as the dir
# to mode it somewhere add after the tar command
# mv $backupname.$date.tar.gz /some/other/dir

#!/bin/bash

read -p "Please input the name of the directory you want back up : " directoryName
echo "You entered $directoryName"

for backupname in $(find -type d -name "$directoryName")
do
      date=$(/bin/date +"%Y_%h_%d_%T")
      tar cvfz $backupname.$date.tar.gz $backupname
done
Avatar of Polaris2k9
Polaris2k9

ASKER

Wow Oren, thank you so much, thats exactly what I was looking for. And yes, you're right, I think I do need some way of checking for duplicate directories.

So if multiple duplicate directories are found, is there any way to just make the script output all occurrences of the directory and prompt the user to choose which one is the right one?
#!/bin/bash
#
date=$(/bin/date +"%Y_%h_%d_%T")

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 : " directoryName
        echo "tar cfz $directoryName.$date.tar.gz $directoryName"
      exit 0
fi

for backupname in $(find -type d -name "$directoryName")
do

      echo "tar cvfz $backupname.$date.tar.gz $backupname"
done
#SORRY thats the one
 Was this comment helpful?
Yes No
0ren:
#!/bin/bash
#
date=$(/bin/date +"%Y_%h_%d_%T")

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 : " directoryName
      tar cfz $directoryName.$date.tar.gz $directoryName
      exit 0
else
      tar cvfz $directoryName.$date.tar.gz $directoryName
fi


Oren you're a legend, thank you so much. I just have one more question. I'm having problems with tar and the date function, when I try and run the above script I always seem to get a lot of errors, see below.

Please input the name of the directory you want back up : testdirectory
You entered testdirectory
tar: Cannot execute remote shell: No such file or directory
tar: testdirectory.2009_May_19_18\:26\:53.tar.gz: Cannot open: Input/output error
tar: Error is not recoverable: exiting now
testdirectory/
tar: Child returned status 2
tar: Error exit delayed from previous errors

The directory is there, is it something to do with the backslashes and the date format?
change the script first line to
#!/bin/bash -xv

run it and send the output
it seems for some reason an remote shell involved. maybe an alias

Please input the name of the directory you want back up : testdirectory
You entered testdirectory
tar: Cannot execute remote shell: No such file or directory
tar: testdirectory.2009_May_19_18\:26\:53.tar.gz: Cannot open: Input/output error
tar: Error is not recoverable: exiting now
testdirectory/
tar: Child returned status 2
tar: Error exit delayed from previous errors

I was having similar errors with tar today
also change the name of the date variable from date  to today or something else
I've tried it by removing the date variable alltogether and get the same problems
mmm.
did you change the first script line ?
it suppose to show a lot of text
the script should begine like :

#!/bin/bash -xv
#
date=$(/bin/date +"%Y_%h_%d_%T")

read -p "Please input t......

is it ?
Yeah the shebang and first few lines are exactly the same as the code you provided, even straight copy + pasting your code doesn't seem to work.
another thing
enter before the tar command \ or insert the full path so no alias will be invloved
/bin/tar
or
\tar
i think the porblem is related to the backslash in the tar command
change the date variable to
/bin/date +"%Y_%h_%d_%H:%M:%S"
oops
date=$(/bin/date +"%Y_%h_%d_%H:%M:%S")
ahhh, adding \tar worked perfectly, But that is with $date removed from the tar lines, the instant I tried adding $date back I started getting the following errors....


Please input the name of the directory you want back up : testdirectory
You entered testdirectory
tar: Cannot execute remote shell: No such file or directory
tar: testdirectory.2009_May_19_18\:50\:07.tar.gz: Cannot open: Input/output error
tar: Error is not recoverable: exiting now
testdirectory/
tar: Child returned status 2
tar: Error exit delayed from previous errors
mv: cannot stat `testdirectory.2009_May_19_18:50:07.tar.gz': No such file or directory
did you tried the date thing ?
changing it to
date=$(/bin/date +"%Y_%h_%d_%H:%M:%S")
Yeah just tried the new date function. Still get

Please input the name of the directory you want back up : testdirectory
You entered testdirectory
tar: Cannot execute remote shell: No such file or directory
tar: testdirectory.2009_May_19_18\:52\:36.tar.gz: Cannot open: Input/output errortestdirectory/

tar: Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error exit delayed from previous errors
mv: cannot stat `testdirectory.2009_May_19_18:52:36.tar.gz': No such file or directory
it doesnt make sense
where did the \ came from ?
can you post the script ? ill llook into it
#!/bin/bash -xv
#
date=$(/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 : " directoryName
      \tar cfz $directoryName.$date.tar.gz $directoryName
      exit 0
else
      \tar cvfz $directoryName.$date.tar.gz $directoryName

mv $directoryName.$date.tar.gz $HOME/backups

fi


Really hope I haven't done anything silly to it :(   Thank you SO much for all your help, You've been wonderful Oren.
ASKER CERTIFIED SOLUTION
Avatar of 0ren
0ren
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Absolutely brilliant, Extremely helpful and prompt. Thank you very very much for walking me through the script. Couldn't have asked for someone more helpful.
you welcome
anytime