Skip to content

Instantly share code, notes, and snippets.

@ErykDarnowski
Last active March 2, 2026 18:07
Show Gist options
  • Select an option

  • Save ErykDarnowski/058388955564d08ce6cadefb820ccade to your computer and use it in GitHub Desktop.

Select an option

Save ErykDarnowski/058388955564d08ce6cadefb820ccade to your computer and use it in GitHub Desktop.
Quick & dirty hugo website automatic build + upload shell script.
#!/bin/bash
#
# **A quick & dirty hugo website build + upload script.**
# Place this file in your hugo website's root directory, run `chmod +x`
# Setup the config variables and enjoy a more streamlined experience!
FTP_LOGIN=""
FTP_PASS=""
FTP_HOST=""
LOCAL_PATH="$(realpath ./public/)"
REMOTE_PATH=""
BATCH_FILE="/tmp/sftp_batch.txt"
## Clean up local
rm -rf ./public ./resources
## Build hugo output + check
hugo --minify || { echo "Hugo build failed!"; exit 1; }
## Clean up remote
> "$BATCH_FILE"
# First pass: delete all files depth-first
while IFS= read -r -d '' item; do
rel="${item#$LOCAL_PATH/}"
if [ -f "$item" ]; then
echo "rm \"$REMOTE_PATH/$rel\"" >> "$BATCH_FILE"
fi
done < <(find "$LOCAL_PATH" -mindepth 1 -depth -print0)
# Second pass: remove directories depth-first
while IFS= read -r -d '' item; do
rel="${item#$LOCAL_PATH/}"
if [ -d "$item" ]; then
echo "rmdir \"$REMOTE_PATH/$rel\"" >> "$BATCH_FILE"
fi
done < <(find "$LOCAL_PATH" -mindepth 1 -depth -print0)
echo "bye" >> "$BATCH_FILE"
sshpass -p "$FTP_PASS" sftp -o StrictHostKeyChecking=no \
"$FTP_LOGIN@$FTP_HOST" < "$BATCH_FILE"
## Upload to server
sshpass -p "$FTP_PASS" scp -rCv $LOCAL_PATH/* $FTP_LOGIN@$FTP_HOST:"$REMOTE_PATH/"
## Clean up local (again)
rm -rf ./public ./resources $BATCH_FILE
## Notify user
echo -e "\nDone!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment