Pages

Wednesday, 19 June 2019

The most confused Linux commands ---- rsync

rsync

The most common correct example is:

rsync -az --delete  user01@host1:/app/project1/   /app/project1/

But can you tell the difference between the following four commands?

rsync -az --delete  user01@host1:/app/project1   /app/project1
rsync -az --delete  user01@host1:/app/project1   /app/project1/
rsync -az --delete  user01@host1:/app/project1/   /app/project1
rsync -az --delete  user01@host1:/app/project1/   /app/project1/
rsync -az --delete  user01@host1:/app/project1    /app
rsync -az --delete  user01@host1:/app/project1    /app/

The first 2 commands won't do what you expected.

The trailing slash play a significant role in Source Path:
  • host1:/app/project will copy the "project" and it's files and sub folders  to under the destination dir.
  • host1:/app/project/ will copy the files and folders under /app/project/ to under the destination dir.
The trailing slash play a role in Destination Path only if the source is a file rather than folder.


If the source path is not a file, the destination path is always treated as a folder.
If the source path is a file, the destination path can be treated as a folder or file.

e.g.

rsync user01@host1:/app/1.txt  A

This command depends on whether "A" exists and whether it's a file or folder when it's running.
  •  If A doesn't exist, 
    • rsync will act like "cp host1:/app/1.txt A", so A will be a file name with content of 1.txt.

  • If A exists,
    • if A is a dir, then rsync will copy 1.txt to A/1.txt
    • if A is a file, then rsync will overwrite the content of A to the content of 1.txt, as if "cp -f 1.txt A" 
rsync user01@host1:/app/1.txt  A/

This commands will create direcotry "A" if it doesn't exist. So "A" will always be a folder rather than a file.


No comments:

Post a Comment