[Linux](EN) Copy files to local or remote using rsync
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
andscp
. It can copy file and directory to not only remote but also local. - There are so many options that can be combined for many functions.(preserve symlink, preserve permission, preserve timestamp etc)
Basic Usage
- Contents from manual page
rsync [options ...] [source] [target]
옵션
-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 if for directory copy-l
: Copy symlinks as symlinks-p
: Preserve permissions-t
: Preserve modification times-g
: Preserve group-o
: Preserve owner(super-user only)-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 fifos.
Examples
- Local file
# rsync [File Name] [Target Path]
rsync -avzh test_file.txt /tmp
- Local directory
# rsync [Directory Name] [Target Path]
rsync -avzh test_directory /tmp
# Copy all files in directory
# rsync [Directory Name]/ [Target Path]
rsync -avzh test_directory/ /tmp
- Remote file
# rsync [File Name] [User]@[IP Address]:[Path]
rsync -avzh test.txt twpower-private-server:~
rsync -avzh test.txt twpower@192.168.1.2:~
- Remote directory
# rsync [Directory Name] [User]@[IP Address]:[Path]
rsync -avzh test.txt twpower-private-server:~
rsync -avzh test.txt twpower@192.168.1.2:~
# Copy all files in directory
# rsync [Directory Name]/ [User]@[IP Address]:[Path]
rsync -avzh test_directory/ twpower-private-server:~
rsync -avzh test_directory/ twpower@192.168.1.2:~