2

I am trying to rsync files from a directory incoming to a directory called outgoing, i.e.,

/testcopy/folder1/incoming/test1.txt

to

/testdest/folder1/outgoing/

Directory source:

/testcopy/folder1/incoming/test1.txt
/testcopy/folder1/incoming/test2.txt
/testcopy/folder2/incoming/test1.txt
/testcopy/folder2/incoming/test2.txt
/testcopy/folder3/incoming/test1.txt
/testcopy/folder3/incoming/test2.txt

Directory destination:

/testdest/folder1/outgoing/
/testdest/folder2/outgoing/
/testdest/folder3/outgoing/

What I want the destination to look like:

/testdest/folder1/outgoing/test1.txt
/testdest/folder1/outgoing/test2.txt
/testdest/folder2/outgoing/test1.txt
/testdest/folder2/outgoing/test2.txt
/testdest/folder3/outgoing/test1.txt
/testdest/folder3/outgoing/test2.txt

The rsync script I have tried:

touch /testcopy/folder3/incoming/test4.txt

What I would expect to see is test4.txt file under the /testdest/folder3/outgoing/

# rsync -av /testcopy/*/incoming/* /testdest/*/outgoing/
sending incremental file list

sent 520 bytes  received 12 bytes  1,064.00 bytes/sec
total size is 0  speedup is 0.00

I have tried a couple different iteration of the above script, but cannot seem to get it right.

0

rsync does not allow rewriting of paths between source and destination.

What you could do instead is to call rsync once for each /testcopy/*/incoming directory:

for srcdir in /testcopy/*/incoming/; do
    [ ! -d "$srcdir" ] && break

    destdir=/testdest/${srcdir#/testcopy/}   # replace /testcopy/ with /testdest/
    destdir=${destdir%/incoming/}/outgoing/  # replace /incoming/ with /outgoing/

    mkdir -p "$destdir" &&
    rsync -av "$srcdir" "$destdir"
done

For each incoming directory, a destination path is constructed by replacing the /testcopy/ path prefix with /testdest/ and the /incoming/ path suffix with /outgoing/. This is done using two standard parameter substitutions.

The loop also creates the destination directory just in case it does not already exist.

The [ ! -d "$srcdir" ] && break at the start of the loop makes sure that the mkdir and rsync isn't run if the pattern does not match anything (the shell would by default leave the pattern unexpanded, unless you're in zsh). In bash, you may want to use shopt -s nullglob instead, before the loop.

2
  • Thank you one question, is this line "mkdir -p "$destdir" &&" recreating the the /testdest/ directory?
    – Ron Russey
    Jan 19 '20 at 16:53
  • @RonRussey Only if it does not already exist. If any of the directories in the path /testdest/folder1/outgoing was missing, the rsync command would fail to copy the contents of /testcopy/folder1/incoming into it.
    – Kusalananda
    Jan 19 '20 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.