Register
It is currently Thu Apr 24, 2014 12:13 am

An automated script to copy directories and check them


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Fri Dec 04, 2009 4:02 pm   

Joined: Fri Dec 04, 2009 3:46 pm
Posts: 1
So this might be really simple but I'm having some issues with it.

What I need the script to do is:

1) ask what directories you would like to copy (multiple directories need to be copied)
2) ask to what location these directories should be copied (all going to same location)
3) check to see if the destination directory and sub folders exits (guess this would be best if it used the diff command)
4) if it doesn't exist then copy over the directory recursively
5) diff the directory again to make sure everything the same
6) if its not then rinse and repeat
7) move on to the next directory and do the same thing
8) exit nicely

ps. it would be nice if when executing the script you could pass an argument to just copy one directory right on the command line without it asking what directory and where. something like

usage: script [OPTIONS] <source dir> <destination dir>
-s copies single directory
-m multiple directories (will prompt for source dirs and destination dir)


Thanks in advance everyone!!


Top
 Profile  
 PostPosted: Thu Dec 17, 2009 9:34 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
I don't see the point in trying to diff the directories if the content is recursively moved.
It won't lose any files on the way.
Code:
#!/bin/bash
OPT=$1
SRC=$2
DST=$3

function usage {
   echo "USAGE: $0 <-s|-m> [<source directory>] [<destination directory>] "
   exit 0
}

function copy_dir {
   SRC_DIR=$1
   DST_DIR=$2
   [ -d $DST_DIR ] && exit 1

   # Add things here if you want extra features :)
   cp -R $SRC_DIR $DST_DIR
   # this takes the return code from "cp" and exits if it's not 0
   [ $? -eq 0 ] || exit $?
}

case $OPT in
   -s)
      copy_dir $SRC $DST
   ;;
   -m)
      while [ true ]; do
         echo "Write \"exit\" to exit program"
         read -p "Source directory: " SRC
         case $SRC in
            [eE][xX][iI][tT]|[xX])
            exit 0
            ;;
         esac
         read -p "Destination directory: " DST
         copy_dir $SRC $DST
         echo "- Copy done.";
      done
   ;;
   *)
      usage
   ;;
esac


This pretty much just copies the files the way you want it too.
If you want to make some extra checks and things, then I suggest you edit the function "copy_dir".

In "-m" mode you can type in "exit" (in any case-format) in the "Source directory" field to exit it cleanly.
In "-s" mode it only takes commandline input (source and destination as described in the usage) and then exits

Best regards
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP