1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Compare 2 directories and copy differences to directory 3

Discussion in 'Shell Script' started by andrewschools, Aug 28, 2010.

  1. andrewschools

    andrewschools New Member

    Joined:
    Aug 28, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello there -

    I have three directories. I would like to compare directory1 with directory2, then take those changes/new files and copy them over to directory3. I was given the following code:

    #!/bin/bash

    dir2="d1"
    dir1="d2"
    dir3="d3"

    for file in dir2/*; do
    file_in_dir1=dir1/$(basename ${file})
    if [ ! -e ${file_in_dir1} ]; then
    # If the file in dir2 does not exist in dir1, copy
    cp ${file} dir3
    elif ! diff ${file} ${file_in_dir1}; then
    # if the file in dir2 is different then the one in dir1, copy
    cp ${file} dir3
    fi
    done
    And I'm getting the following error: 7: Syntax error: word unexpected (expecting "do")

    I don't have any experience in shell scripting. Can someone tell me what I need to do to get this script working...

    Thanks!
    Andrew
     

Share This Page