Solved

Bash Script, Search for a given directory

Posted on 2009-05-18
28
581 Views
Last Modified: 2013-12-26
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.
0
Comment
Question by:Polaris2k9
  • 19
  • 9
28 Comments
 
LVL 5

Expert Comment

by:0ren
ID: 24418455
you can look for a directory using find like htis:
find -type d -name dirname

what about a directory name that exist twice ?
0
 
LVL 5

Expert Comment

by:0ren
ID: 24418462
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
0
 
LVL 5

Expert Comment

by:0ren
ID: 24418478
#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"
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 5

Expert Comment

by:0ren
ID: 24418500
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

0
 
LVL 5

Expert Comment

by:0ren
ID: 24418506
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
0
 
LVL 5

Expert Comment

by:0ren
ID: 24418508
and remove the v from the tar command to make it faster ( v for verbose )
tar cfz
0
 
LVL 5

Expert Comment

by:0ren
ID: 24418529
#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
0
 

Author Comment

by:Polaris2k9
ID: 24418570
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?
0
 
LVL 5

Expert Comment

by:0ren
ID: 24418635
#!/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
0
 
LVL 5

Expert Comment

by:0ren
ID: 24418644
#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


0
 

Author Comment

by:Polaris2k9
ID: 24418911
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?
0
 
LVL 5

Expert Comment

by:0ren
ID: 24418940
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

0
 

Author Comment

by:Polaris2k9
ID: 24418957
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
0
 
LVL 5

Expert Comment

by:0ren
ID: 24418965
also change the name of the date variable from date  to today or something else
0
 

Author Comment

by:Polaris2k9
ID: 24418968
I've tried it by removing the date variable alltogether and get the same problems
0
 
LVL 5

Expert Comment

by:0ren
ID: 24418977
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 ?
0
 

Author Comment

by:Polaris2k9
ID: 24418985
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.
0
 
LVL 5

Expert Comment

by:0ren
ID: 24418990
another thing
enter before the tar command \ or insert the full path so no alias will be invloved
/bin/tar
or
\tar
0
 
LVL 5

Expert Comment

by:0ren
ID: 24419010
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"
0
 
LVL 5

Expert Comment

by:0ren
ID: 24419015
oops
date=$(/bin/date +"%Y_%h_%d_%H:%M:%S")
0
 

Author Comment

by:Polaris2k9
ID: 24419030
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
0
 
LVL 5

Expert Comment

by:0ren
ID: 24419036
did you tried the date thing ?
changing it to
date=$(/bin/date +"%Y_%h_%d_%H:%M:%S")
0
 

Author Comment

by:Polaris2k9
ID: 24419042
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
0
 
LVL 5

Expert Comment

by:0ren
ID: 24419058
it doesnt make sense
where did the \ came from ?
can you post the script ? ill llook into it
0
 

Author Comment

by:Polaris2k9
ID: 24419066
#!/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.
0
 
LVL 5

Accepted Solution

by:
0ren earned 500 total points
ID: 24419120
# 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
0
 

Author Closing Comment

by:Polaris2k9
ID: 31582874
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.
0
 
LVL 5

Expert Comment

by:0ren
ID: 24419422
you welcome
anytime
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question