[Linux](EN) Copy file or directory to local or remote using rsync

Update(2021.02.27): Add contents of copying remote file or directory to local

Let’s use rsync command in Linux base OS.


Environment and Prerequisite

  • Linux
  • Bash shell(/bin/bash)


rsync command

Basic

  • rsync: It represents remote sync. It is a fast, versatile, remote (and local) file-copying tool which can use with various options.
  • It is similar file-copying tool like rcp and scp. It can copy file or directory to not only remote but also local. Also, remote file or directory can be copied to local.
  • There are so many options that can be combined to many functions.(preserve symlink, preserve permission, preserve timestamp etc)


Basic Usage

  • Contents from manual page
rsync [options ...] [source] [target]

Option

  • -v: Increase verbosity. It give more information about copying process.
  • -z: Compress files when copying.
  • -h: Print human-readable mode.
  • -a (same as -rlptgoD): Archive mode. It is same as -rlptgoD options. Those options explanation are at below of this option. It copies symlink, permission, timestamp and etc properties.
  • -r: Recurse into directories. Use it for directory copy.
  • -l: Copy symlinks as symlinks.
  • -p: Preserve permissions.
  • -t: Preserve modification times.
  • -g: Preserve group.
  • -o: Preserve owner(super-user only).
  • -e: Specify the remote shell to use.
  • -D (same as --devices --specials): Same as --devices --specials.
  • --devices: This option causes rsync to transfer character and block device files to the remote system to recreate these devices.(root only).
  • --specials: This option causes rsync to transfer special files such as named sockets and fifo.
  • -P (same as --partial --progress): Same as --partial --progress.
  • --partial: By default, rsync will delete any partially transferred file if the transfer is interrupted. This option tells rsync to keep the partial file which should make a subsequent transfer of the rest of the file much faster.
  • --progress: Showing the progress of the transfer.


Examples

  • Copy file to local
# rsync [File Name] [Target Path]

rsync -avzhP test.txt /tmp


# Name can be changed during transfer

rsync -avzhP test.txt /tmp/test-renamed.txt
  • Copy directory to local
# rsync [Directory Name] [Target Path]
# Directory itself is copied.

rsync -avzhP test-directory /tmp


# Copy all files and subdirectories in directory
# rsync [Directory Name]/ [Target Path]

rsync -avzhP test-directory/ /tmp
  • Copy file to remote
# rsync [File Name] [User]@[IP Address]:[Path]

rsync -avzhP test.txt twpower-private-server:~
rsync -avzhP test.txt twpower@192.168.1.2:~


# Name can be changed during transfer

rsync -avzhP test.txt twpower-private-server:~/test-renamed.txt
rsync -avzhP test.txt twpower@192.168.1.2:~/test-renamed.txt
  • Copy directory to remote
# rsync [Directory Name] [User]@[IP Address]:[Path]
# Directory itself is copied.

rsync -avzhP test-directory twpower-private-server:~
rsync -avzhP test-directory twpower@192.168.1.2:~


# Copy all files and subdirectories in directory
# rsync [Directory Name]/ [User]@[IP Address]:[Path]

rsync -avzhP test-directory/ twpower-private-server:~
rsync -avzhP test-directory/ twpower@192.168.1.2:~
  • Copy file from remote to local
# rsync [User]@[IP Address]:[File Name] [Path]

rsync -avzhP twpower-private-server:~/test.txt .
rsync -avzhP twpower@192.168.1.2:~/test.txt .


# Name can be changed during transfer

rsync -avzhP twpower-private-server:~/test.txt ./test-renamed.txt
rsync -avzhP twpower@192.168.1.2:~test.txt ./test-renamed.txt
  • Copy directory from remote to local
# rsync [User]@[IP Address]:[Directory Name] [Path]
# Directory itself is copied.

rsync -avzhP twpower-private-server:~/test-directory .
rsync -avzhP twpower@192.168.1.2:~/test-directory .


# Copy all files and subdirectories in directory
# rsync [User]@[IP Address]:[Directory Name]/ [Path]

rsync -avzhP twpower-private-server:~/test-directory/ .
rsync -avzhP twpower@192.168.1.2:~/test-directory/ .


Additional Things


Reference