Skip to content

Instantly share code, notes, and snippets.

@hzbd
Last active August 14, 2025 10:09
Show Gist options
  • Select an option

  • Save hzbd/577055a99b58d272f6e0c3d88e6e3074 to your computer and use it in GitHub Desktop.

Select an option

Save hzbd/577055a99b58d272f6e0c3d88e6e3074 to your computer and use it in GitHub Desktop.
transfer.sh - Securely transfer files/directories using rsync over SSH. Supports resumable transfers and colored output.
#!/bin/bash
# transfer.sh - Securely transfer files/directories using rsync over SSH.
# Supports resumable transfers and colored output.
# --- Define Colors ---
GREEN='\033[0;32m'
RED='\033[0;31m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# --- Script Arguments ---
MODE="$1"
SOURCE="$2"
DESTINATION="$3"
PORT=${4:-22} # Default to SSH port 22
# --- Argument Validation ---
if [[ "$MODE" != "send" && "$MODE" != "get" ]] || [ "$#" -lt 3 ]; then
echo -e "${RED}❌ Error: Invalid arguments.${NC}"
echo -e "\nUsage: $0 [send|get] [source] [destination] [port]"
echo " ex: $0 send ./local-file user@host:/remote/path"
echo " ex: $0 get user@host:/remote/file ./local-path 2222"
exit 1
fi
# rsync options: -a (archive), -v (verbose), -z (compress), -P (progress/partial)
RSYNC_OPTS="-avzP"
# --- Execute rsync command based on mode ---
case "$MODE" in
"send")
if [ ! -e "$SOURCE" ]; then
echo -e "${RED}❌ Error: Local source '$SOURCE' not found.${NC}"
exit 1
fi
echo -e "${CYAN}πŸš€ Sending '$SOURCE' to '$DESTINATION' (port $PORT)...${NC}"
rsync $RSYNC_OPTS -e "ssh -p $PORT" "$SOURCE" "$DESTINATION"
;;
"get")
echo -e "${CYAN}πŸš€ Getting '$SOURCE' from '$DESTINATION' (port $PORT)...${NC}"
rsync $RSYNC_OPTS -e "ssh -p $PORT" "$SOURCE" "$DESTINATION"
;;
esac
# --- Check the result ---
if [ $? -eq 0 ]; then
echo -e "${GREEN}βœ… Success! Transfer complete.${NC}"
else
echo -e "${RED}❌ Failed. Please check the following:${NC}"
echo " - Connectivity, credentials, paths, and permissions."
echo " - SSH is running on port $PORT and 'rsync' is installed on the server."
fi
@hzbd
Copy link
Author

hzbd commented Aug 14, 2025

$ transfer --help

Usage: /usr/local/bin/transfer [send|get] [source] [destination] [port]
  ex: /usr/local/bin/transfer send ./local-file user@host:/remote/path
  ex: /usr/local/bin/transfer get user@host:/remote/file ./local-path 2222

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment