These two commands rcp and rmv acts a direct replacement for cp and mv for copying or moving files using rsync.
Instead of doing:
rsync -avh --partial --info=progress2 source dest
rsync -avh --partial --info=progress2 --remove-source-files source dest
Now you can simply do:
rcp source dest
rmv source dest
You can also include additional rsync options, here are two examples with -z for compression (great for faster network transfers) and --dry-run to see what gets transferred without actually transferring (yet).
rcp -z source dest
rmv --dry-run source dest
The rcp() and rmv() shell functions
You can copy and paste the following lines in your .zshrc or .bashrc file.
# On macOS, the default `rsync` tool does not support the
# `--info=progress2` command, so run `brew install rsync`
# to get the latest rsync.
alias rsync="/opt/homebrew/bin/rsync"
# Function to copy files
# Usage: rcp [rsync_options] source destination
rcp() {
local options="-avh --partial --info=progress2"
local source_path="$1"
local dest_path="$2"
if [[ -n "$2" ]]; then
shift 2
rsync $options "$@" "$source_path" "$dest_path"
else
echo "Usage: rcp [rsync_options] source destination"
return 1
fi
}
# Function to move files and delete empty directories
# Usage: rmv [rsync_options] source destination
rmv() {
local options="-avh --partial --info=progress2 --remove-source-files"
local source_path="$1"
local dest_path="$2"
# Shift off the first two arguments (source and destination)
# This leaves any remaining arguments to be passed as rsync_options
if [[ -n "$2" ]]; then
shift 2
rsync $options "$@" "$source_path" "$dest_path" && \
find "$source_path" -type d -empty -delete
else
echo "Usage: rmv [rsync_options] source destination"
return 1
fi
}
macOS users: Upgrade your rsync
The default rsync tool that comes bundled with macOS is v2.6.9 does not support the --info=progress2 command, so run brew install rsync to get the latest rsync.
โฏ /usr/bin/rsync --version
openrsync: protocol version 29
rsync version 2.6.9 compatible
โฏ /opt/homebrew/bin/rsync --version
rsync version 3.4.1 protocol version 32
Handling the removal of empty directories
The --remove-source-files option in rsync does not remove empty directories after all the files has been moved. Therefore, in the rmv() shell function above, you will notice that it runs an additional command to remove the empty directories using find after rsync has finished running.
So there you have it! These two little shell functions, rcp and rmv, wrapping up the power of rsync into something as easy to use as cp and mv.
No more remembering long strings of options, just a simple command that gets the job done, with a nice progress bar to boot.
