Results 1 to 6 of 6

Thread: Scripting Help

  1. #1
    Join Date
    Dec 2009
    Beans
    6

    Talking Scripting Help

    Hello,

    I'm trying to write a bin/bash command that looks at two separate directories and determine if the files date/time stamps are different. If the files date/time are different then copy files over to my backup folder. If not, then don't copy files.

    I know there are tools out there that do this, but I want to learn how to do this by using command line.

    Your help is much appreciated!

    Thanks,

  2. #2
    Join Date
    Jun 2005
    Location
    South Dakota, US
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: Scripting Help

    You shouldn't try to do this yourself because the command line isn't meant for complex operations such as this. Your best option would be to use rsync and make your script a wrapper around this. I use rsync in a large number of my scripts.

    rsync -au --delete ---progress
    ^ I use this a lot

  3. #3
    Join Date
    Feb 2008
    Location
    London, On, Canada
    Beans
    88

    Re: Scripting Help

    If you want to learn why are you asking others to do it? There are guides for this sort of thing. A much better learning choice than asking other to do it for you. I'd tell you how right now but i've never checked the time stamp of a file. Also, some of those tools are probably open source. A good start if you wanna figure out how it works.

  4. #4
    Join Date
    Jun 2005
    Location
    South Dakota, US
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: Scripting Help

    Quote Originally Posted by serpantman View Post
    If you want to learn why are you asking others to do it? There are guides for this sort of thing. A much better learning choice than asking other to do it for you. I'd tell you how right now but i've never checked the time stamp of a file. Also, some of those tools are probably open source. A good start if you wanna figure out how it works.
    You can easily do "apt-get source rsync" if you have the source repositories enabled and you will get the full package source. You could traverse that code and see how they handle it... There's also #bash on Freenode.

    Like I said though; there's a reason people rely on code that exists. Reproducing efforts usually doesn't teach you as much as doing something completely new.

  5. #5
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Scripting Help

    Quote Originally Posted by mtecknology View Post
    You shouldn't try to do this yourself because the command line isn't meant for complex operations such as this.
    this is crap. what you do on the command line can be put into a script AND you can do complex task with it.

  6. #6
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Scripting Help

    minimalistic solution, if you have bash 4.0+

    Code:
    dir1="/home/dir1"
    dir2="/home/dir2"
    backup="/dest"
    shopt -s globstar
    for files in $dir1/**
    do
        basename=${files##*/}
        if [ -f "$files" ] && [ -e "$dir2/$basename" ];then
           if [ "$files" -nt "$dir2/$basename" ] || [ "$files" -ot "$dir2/$basename" ];then
                echo cp "$files" "$dir2/$basename" $backup
           fi
        fi
    
    done

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •