It is currently Thu May 24, 2018 8:06 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!!

 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.

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

function copy_dir {
   [ -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
      copy_dir $SRC $DST
      while [ true ]; do
         echo "Write \"exit\" to exit program"
         read -p "Source directory: " SRC
         case $SRC in
            exit 0
         read -p "Destination directory: " DST
         copy_dir $SRC $DST
         echo "- Copy done.";

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

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: Google [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