Transfer files between Unix machines with rsync
Transfer files from local to remote
Syntax (explainshell):
rsync -LPa <local_path> <remote_path>
-L, --copy-links
: when encountering symlinks, copy the actual directories / files instead of the symlink.-P, --progress
: show progress info.-a, --archive
: recursively copy files.
1. Configure SSH aliases
Configure SSH aliases to make file transfers easier. If you don’t, you’ll have to type the full IP address instead of an alias (e.g., sajad@172.12.34.56:some-path
instead of just prod:some-path
).
2. Create test files and directories
Let’s test the rsync setup by first transferring a test file. Create local file:
# Local machine
mkdir /tmp/rsync-local
echo 'Test file' > /tmp/rsync-local/file.txt
Open a new terminal tab, SSH into remote machine, and create remote directory:
# Remote machine
mkdir /tmp/rsync-remote
cd /tmp/rsync-remote
3. Make a test transfer
Run:
# Local machine
rsync -LPa /tmp/rsync-local/file.txt <ssh-alias>:/tmp/rsync-remot
Now, jump into your remote machine and you should see the file has been transferred:
# Remote machine
cat /tmp/rsync-remote/file.txt
Transfer files from remote to local
Transferring from remote to local is the same process, except you specify the remote path first. So, something like this:
rsync -LPa <remote_path> <local_path>
Bonus: use bash alias
Add this alias to whatever file you use for aliases (e.g., .zshrc
, bashrc
):
alias transfer="rsync -LPa"
Now, you can do something like this:
transfer /tmp/foo.txt prod:/tmp/
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment