Solved

Bash Script, Search for a given directory

Posted on 2009-05-18
28
567 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
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)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now