Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 606
  • Last Modified:

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.
0
Polaris2k9
Asked:
Polaris2k9
  • 19
  • 9
1 Solution
 
0renCommented:
you can look for a directory using find like htis:
find -type d -name dirname

what about a directory name that exist twice ?
0
 
0renCommented:
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
 
0renCommented:
#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
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
0renCommented:
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
 
0renCommented:
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
 
0renCommented:
and remove the v from the tar command to make it faster ( v for verbose )
tar cfz
0
 
0renCommented:
#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
 
Polaris2k9Author Commented:
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
 
0renCommented:
#!/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
 
0renCommented:
#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
 
Polaris2k9Author Commented:
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
 
0renCommented:
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
 
Polaris2k9Author Commented:
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
 
0renCommented:
also change the name of the date variable from date  to today or something else
0
 
Polaris2k9Author Commented:
I've tried it by removing the date variable alltogether and get the same problems
0
 
0renCommented:
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
 
Polaris2k9Author Commented:
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
 
0renCommented:
another thing
enter before the tar command \ or insert the full path so no alias will be invloved
/bin/tar
or
\tar
0
 
0renCommented:
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
 
0renCommented:
oops
date=$(/bin/date +"%Y_%h_%d_%H:%M:%S")
0
 
Polaris2k9Author Commented:
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
 
0renCommented:
did you tried the date thing ?
changing it to
date=$(/bin/date +"%Y_%h_%d_%H:%M:%S")
0
 
Polaris2k9Author Commented:
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
 
0renCommented:
it doesnt make sense
where did the \ came from ?
can you post the script ? ill llook into it
0
 
Polaris2k9Author Commented:
#!/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
 
0renCommented:
# 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
 
Polaris2k9Author Commented:
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
 
0renCommented:
you welcome
anytime
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 19
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now