Search code examples
rsyncfile-copying

rsync: copy selected files from same directory into destination in one sync


How to copy selected/specific files from source into destination directory using rsync?

my source structure:

src ->|
      |--> file1.jpg
      |--> file2.jpg
      |--> file3.jpg

dest->|
      |--> file1.jpg
      |--> file2.jpg

i want to copy only file1.jpg and file2.jpg into destination in single rsync transaction?

what i tried,

rsync -avcz --file-from=file1.jpg,file2.jpg src/ dest/ # since it didnt work i did through looping

loop:

rsync -avcz src/file1.jpg dest/

i dont know how to make it simple?


Solution

  • As rsync is very flexible there is more than one way to accomplish what you are trying to do.

    One solution that comes to mind

    pushd src/ ; rsync -avcz file1.jpg file2.jpg ../dest/ ; popd
    

    Or using shell expansion

    pushd src/ ; rsync -avcz file{1,2}.jpg ../dest/ ; popd
    

    It is more robust to use a subshell instead of the pushd-popd idiom since with a subshell the command would not get stuck in a subdirectory if rsync fails

    ( cd src/ ; rsync -avcz file{1,2}.jpg ../dest/ )
    

    Or with a modern rsync version it is possible to avoid changing directories because the ./ syntax (available since rsync version 2.6.7) lets one avoid sending the src directory name

    rsync -avcz src/./file{1,2}.jpg dest/