rsync (Remote Sync) is a most commonly used command for copying an synchronizing files and directories to local or remote on linux or mac.
If the source directory has a trailing slash, the command will copy only the directory contents to the destination directory.
When the trailing slash is omitted,
rsync copies the source directory inside the destination directory.
rsync <options> <src> <dest>
How to install rsync command?
To install rsync on linux or ubuntu:
# Install Rsync on Ubuntu and Debian sudo apt install rsync # Install Rsync on CentOS and Fedora sudo yum install rsync # install rsync on mac using brew brew install rsync # install rsync on mac using port sudo port install rsync
How to copy a file from one to other location on local machine?
To copy file from one to other location on local maching using rsync use following commands:
# copy laravel.log file from /var/log directory to /tmp directory # If the destination directory doesn’t exist, rsync will create it. rsync -a /var/log/laravel.log /tmp/
How to copy a file from local to remote location?
rsync must be installed on both the source and the destination machine in order for sync to work. Let's look at following example that copies file from local machine to remote machine:
# copy laravel.log file from /var/log directory to /tmp directory on remote server rsync -a /var/log/laravel/ user@remote_host_or_ip:/tmp/ # copy laravel.log file from /var/log directory to /tmp directory on remote server # specify port if it is different than 22 rsync -a -e "ssh -p 2322" /var/log/laravel/ user@remote_host_or_ip:/tmp/
rsync ignore files or directories when syncing
Sometimes you may not want certain files to be copied or synced. In that case you can use following flag to exclude certain files or directories.
# copy contents of src/ dir to dest/ directory exclude node_modules dir and tmp directory rsync -a --exclude=node_modules --exclude=tmp /src/ /dest/ # create a file called /exclude-file.txt and add name of the directories you want to exclude node_modules tmp dist # use following command to specify file that ignores different directories rsync -a --exclude-from='/exclude-file.txt' /src/ /dest/
|rsync -zvh db.tar.gz /tmp/backups/
|sync a single file on a local machine from one location to another location
|rsync -avzh /root/bkp /tmp/backups/
|sync all the files from one directory to a different directory
|rsync -avzh /root/bkp root@IP:/tmp/
|sync a directory from a local machine to a remote machine
|rsync -avzh root@IP:/tmp /tmp/
|sync a remote directory to a local directory
|rsync -avzhe ssh root@IP:/tmp/*.log /tmp
|Copy a file from a Remote Server to a Local Server with SSH
|rsync -avzhe ssh /tmp/*.log root@IP:/tmp/
|Copy a file from a Local Server to a Remote Server with SSH
|rsync -avz --delete root@IP:/tmp/ /tmp/
|If a file or directory does not exist at the source, but already exists at the destination, you might want to delete that existing file/directory at the target while syncing.
|suppress non-error messages
|skip based on checksum, not mod-time & size
|recurse into directories
|use relative path names
|skip files that are newer on the receiver
|transfer directories without recursing
|copy symlinks as symlinks
|transform symlink into referent file/dir
|preserve hard links
|preserve ACLs (implies -p)
|preserve modification times
|compress file data during the transfer
|exclude files matching PATTERN
|read exclude patterns from FILE