Skip to content

Instantly share code, notes, and snippets.

@Aetherinox
Last active May 23, 2025 09:57
Show Gist options
  • Save Aetherinox/c4ae5d6a5376979f0c80df10a4c9c239 to your computer and use it in GitHub Desktop.
Save Aetherinox/c4ae5d6a5376979f0c80df10a4c9c239 to your computer and use it in GitHub Desktop.
Transfer.sh - Linux Alias (gpg, tar.gz support)
# #
# Transfer.sh > Upload
#
# This alias allows you to push files to your transfer.sh server.
# Supports GPG encryption, and tar.gz or zip compression.
#
# Needs to be placed in
# /home/$USER/.bashrc
# After adding, refresh the file using:
# source /home/$USER/.bashrc
#
# You must have the required packages installed on your system:
# sudo apt install rar unrar zip
#
# @url https://github.com/dutchcoders/transfer.sh
# @usage upload-sh --help
# upload-sh --target file|folder
# upload-sh --target folder --type zip
# upload-sh --target file --type rar --encrypt --password "YourPassword" --output "CustomFile"
# upload-sh --target file|folder --server https://transfer.domain.lan
# upload-sh --target file|folder --encrypt --passphrase "PasswordHere"
# upload-sh --target file|folder --encrypt --passphrase "PasswordHere" --output NewFileName
# #
alias {tsh-upload,tsh.upload,transfer-upload,file-upload,upload-file,sh-upload,upload-sh}="transfer_upload"
function transfer_upload
{
local cmd_app="Transfer.sh"
local cmd_title="Upload $cmd_app"
local cmd_about="Allows you to compress and upload a file to $cmd_app"
local cmd_alias="tsh-upload,tsh.upload,transfer-upload,file-upload,upload-file,sh-upload,upload-sh"
local cmd_build="05-25-2025"
# #
# define > general
# #
local app_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" # path where script was last found in
local app_dir_this_dir="${PWD}" # current script directory
# #
# define > args
# #
local tsh_arg_target=
local tsh_arg_encrypt=false
local tsh_arg_encrypt_pass=
local tsh_arg_output=
local tsh_arg_uploadto="https://transfer.domain.lan"
local tsh_arg_ext_tar="tar.gz"
local tsh_arg_ext_zip="zip"
local tsh_arg_ext_rar="rar"
local tsh_arg_ext_gpg="gpg"
local tsh_arg_compression_enabled=false
local tsh_arg_compression_type="tar"
while [ $# -gt 0 ]; do
case "$1" in
-v|--ver|--version)
echo -e
printf " ${c[blue]}${cmd_title}${c[end]}\n" 1>&2
printf " ${c[grey2]}${cmd_about}${c[end]}\n" 1>&2
printf " ${c[grey1]}last update:${c[end]} ${c[grey1]}$cmd_build${c[end]}\n" 1>&2
echo -e
echo -e
return
;;
-i|--in|-f|--file|-F|--folder|-t|--target)
if [[ "$1" != *=* ]]; then shift; fi
tsh_arg_target="${1#*=}"
;;
-e|--encrypt|--gpg)
tsh_arg_encrypt=true
;;
-p|--pass|--password|--passphrase)
if [[ "$1" != *=* ]]; then shift; fi
tsh_arg_encrypt_pass="${1#*=}"
;;
-o|--out|--output|--output-file)
if [[ "$1" != *=* ]]; then shift; fi
tsh_arg_output="${1#*=}"
;;
-c|--compress)
tsh_arg_compression_enabled=true
;;
-a|--archive|--archive-type|-T|--compress-type|--type)
if [[ "$1" != *=* ]]; then shift; fi
tsh_arg_compression_type="${1#*=}"
;;
-s|--server|-u|--upload-to|--upload)
if [[ "$1" != *=* ]]; then shift; fi
tsh_arg_uploadto="${1#*=}"
;;
-h|--help)
echo -e
printf " ${c[blue]}${cmd_title}${c[end]}\n" 1>&2
printf " ${c[grey2]}${cmd_about}${c[end]}\n" 1>&2
printf " ${c[grey1]}last update:${c[end]} ${c[grey1]}$cmd_build${c[end]}\n" 1>&2
printf " ${c[fuchsia2]}$FUNCNAME${c[end]} [${c[grey2]}-h|--help${c[end]}] | [{${c[grey2]}-i ${c[yellow]}arg${c[grey2]}|-a${c[end]}}]" 1>&2
echo -e
echo -e
printf ' %-5s %-40s\n' "${c[grey1]}Syntax:${c[end]}" "" 1>&2
printf ' %-5s %-30s %-40s\n' " " "${c[grey1]}Command${c[end]} " "${c[fuchsia2]}$FUNCNAME${c[end]} [${c[grey2]}-option [${c[yellow]}arg${c[end]}]${c[end]}]" 1>&2
printf ' %-5s %-30s %-40s\n' " " "${c[grey1]}Options${c[end]} " "${c[fuchsia2]}$FUNCNAME${c[end]} [${c[grey2]}-h|--help${c[end]}]" 1>&2
printf ' %-5s %-30s %-40s\n' " " " ${c[grey2]}-A${c[end]} " "required" 1>&2
printf ' %-5s %-30s %-40s\n' " " " ${c[grey2]}-A...${c[end]} " "required; multiple can be specified" 1>&2
printf ' %-5s %-30s %-40s\n' " " " ${c[grey2]}[ -A ]${c[end]} " "optional" 1>&2
printf ' %-5s %-30s %-40s\n' " " " ${c[grey2]}[ -A... ]${c[end]} " "optional; multiple can be specified" 1>&2
printf ' %-5s %-30s %-40s\n' " " " ${c[grey2]}{ -A | -B }${c[end]} " "one or the other; do not use both" 1>&2
printf ' %-5s %-30s %-40s\n' " " "${c[grey1]}Arguments${c[end]} " "${c[fuchsia2]}$FUNCNAME${c[end]} [${c[grey2]}-t${c[yellow]} arg${c[grey2]}|--target ${c[yellow]}arg${c[end]}]${c[yellow]} arg${c[end]}" 1>&2
printf ' %-5s %-30s %-40s\n' " " "${c[grey1]}Examples${c[end]} " "${c[fuchsia2]}$FUNCNAME${c[end]} ${c[grey2]}--target${c[yellow]} \"File | Folder\"${c[end]}" 1>&2
printf ' %-5s %-30s %-40s\n' " " "${c[grey1]}${c[end]} " "${c[fuchsia2]}$FUNCNAME${c[end]} ${c[grey2]}--target${c[yellow]} \"Folder\"${c[end]} ${c[grey2]}--type${c[yellow]} \"zip\" ${c[end]}" 1>&2
printf ' %-5s %-30s %-40s\n' " " "${c[grey1]}${c[end]} " "${c[fuchsia2]}$FUNCNAME${c[end]} ${c[grey2]}--target${c[yellow]} \"File\"${c[end]} ${c[grey2]}--compress ${c[grey2]}--type${c[yellow]} \"tar\" ${c[end]}" 1>&2
printf ' %-5s %-30s %-40s\n' " " "${c[grey1]}${c[end]} " "${c[fuchsia2]}$FUNCNAME${c[end]} ${c[grey2]}--target${c[yellow]} \"File | Folder\"${c[end]} ${c[grey2]}--type${c[yellow]} \"rar\" ${c[grey2]}--encrypt ${c[grey2]}--password${c[yellow]} \"YourPassword\"${c[end]} ${c[grey2]}--output${c[yellow]} \"CustomFileName\"${c[end]}" 1>&2
printf ' %-5s %-30s %-40s\n' " " "${c[grey1]}${c[end]} " "${c[fuchsia2]}$FUNCNAME${c[end]} ${c[grey2]}--target${c[yellow]} \"File | Folder\"${c[end]} ${c[grey2]}--server${c[yellow]} \"https://transfer.domain.lan\"${c[end]}" 1>&2
printf ' %-5s %-30s %-40s\n' " " "${c[grey1]}${c[end]} " "${c[fuchsia2]}$FUNCNAME${c[end]} ${c[grey2]}--target${c[yellow]} \"File | Folder\"${c[end]} ${c[grey2]}--encrypt ${c[grey2]}--password${c[yellow]} \"YourPassword\"${c[end]}" 1>&2
echo -e
printf ' %-5s %-40s\n' "${c[grey1]}Aliases:${c[end]}" "" 1>&2
printf ' %-5s %-40s\n' " " "${cmd_alias}${c[end]}" 1>&2
echo -e
printf ' %-5s %-40s\n' "${c[grey1]}Options:${c[end]}" "" 1>&2
printf ' %-5s %-81s %-40s\n' " " "${c[blue2]}-t${c[grey1]},${c[blue2]} --target ${c[yellow]}<string>${c[end]} " "file or folder to compress / upload to transfer.sh server${c[end]}" 1>&2
printf ' %-5s %-81s %-40s\n' " " "${c[blue2]}-e${c[grey1]},${c[blue2]} --encrypt ${c[yellow]}${c[end]} " "encrypt using GPG symmetric encryption before upload${c[end]} ${c[navy]}<default> ${c[peach]}$tsh_arg_encrypt${c[end]}" 1>&2
printf ' %-5s %-81s %-40s\n' " " "${c[blue2]} ${c[grey1]} ${c[blue2]} ${c[yellow]}${c[end]} " " ${c[grey1]}requires using ${c[green]}--password${c[end]}" 1>&2
printf ' %-5s %-81s %-40s\n' " " "${c[blue2]}-p${c[grey1]},${c[blue2]} --password ${c[yellow]}<string>${c[end]} " "password to use for gpg symmetric encryption${c[end]}" 1>&2
printf ' %-5s %-81s %-40s\n' " " "${c[blue2]}-c${c[grey1]},${c[blue2]} --compress ${c[yellow]}${c[end]} " "compress files before upload${c[end]} ${c[navy]}<default> ${c[peach]}$tsh_arg_compression_enabled${c[end]}" 1>&2
printf ' %-5s %-81s %-40s\n' " " "${c[blue2]} ${c[grey1]} ${c[blue2]} ${c[yellow]}${c[end]} " " ${c[grey1]}folders are automatically compressed" 1>&2
printf ' %-5s %-81s %-40s\n' " " "${c[blue2]}-t${c[grey1]},${c[blue2]} --type ${c[yellow]}<string>${c[end]} " "type of compression to use${c[end]} ${c[navy]}<default> ${c[peach]}$tsh_arg_compression_type${c[end]}" 1>&2
printf ' %-5s %-81s %-40s\n' " " "${c[blue2]} ${c[grey1]} ${c[blue2]} ${c[yellow]}${c[end]} " " ${c[grey1]}pick any of the following options:" 1>&2
printf ' %-5s %-30s %-40s\n' " " "${c[blue2]} ${c[grey1]} ${c[blue2]} ${c[yellow]}${c[end]} " " - ${c[green]}tar${c[end]}" 1>&2
printf ' %-5s %-30s %-40s\n' " " "${c[blue2]} ${c[grey1]} ${c[blue2]} ${c[yellow]}${c[end]} " " - ${c[green]}rar${c[end]}" 1>&2
printf ' %-5s %-30s %-40s\n' " " "${c[blue2]} ${c[grey1]} ${c[blue2]} ${c[yellow]}${c[end]} " " - ${c[green]}zip${c[end]}" 1>&2
printf ' %-5s %-81s %-40s\n' " " "${c[blue2]}-o${c[grey1]},${c[blue2]} --output ${c[yellow]}<string>${c[end]} " "specify a file name to use when uploading${c[end]}" 1>&2
printf ' %-5s %-81s %-40s\n' " " "${c[blue2]}-s${c[grey1]},${c[blue2]} --server ${c[yellow]}<string>${c[end]} " "specify the transfer.sh server url to use for uploads${c[end]} ${c[navy]}<default> ${c[peach]}$tsh_arg_uploadto${c[end]}" 1>&2
printf ' %-5s %-81s %-40s\n' " " "${c[blue2]}-v${c[grey1]},${c[blue2]} --version ${c[yellow]}${c[end]} " "current version of this app${c[end]}" 1>&2
printf ' %-5s %-81s %-40s\n' " " "${c[blue2]}-x${c[grey1]},${c[blue2]} --dev ${c[yellow]}${c[end]} " "developer mode; verbose logging${c[end]}" 1>&2
printf ' %-5s %-81s %-40s\n' " " "${c[blue2]}-h${c[grey1]},${c[blue2]} --help ${c[yellow]}${c[end]} " "show this help menu${c[end]}" 1>&2
echo -e
echo -e
return
;;
*)
;;
esac
shift
done
# #
# Target > Is Empty
# #
if [ -z "$tsh_arg_target" ]; then
echo -e
printf '%-29s %-65s\n' " ${c[red2]}ERROR${c[end]}" "${c[end]}Target File or Folder Not Specified${c[end]}"
printf '%-29s %-65s\n' " ${c[yellow]}${c[end]}" "You must specify a valid target which is either a file or folder${c[end]}"
printf '%-34s %-65s\n' " ${c[yellow]}${c[end]}" "${c[fuchsia2]}./$FUNCNAME ${c[grey2]}--target ${c[yellow]}\"folder|file\"${c[end]}"
printf '%-34s %-65s\n' " ${c[yellow]}${c[end]}" "${c[fuchsia2]}./$FUNCNAME ${c[grey2]}--target ${c[yellow]}\"folder|file\"${c[end]} ${c[grey2]}--encrypt${c[end]} ${c[grey2]}--password ${c[yellow]}\"PasswordHere\"${c[end]}"
echo -e
return
fi
# #
# Target > Define Defaults
# #
local file_target="${tsh_arg_target%/}"
local file_output="${tsh_arg_output}"
local file_upload="${tsh_arg_output}"
if [ "$tsh_arg_compression_type" == "tar" ]; then
file_compression=${tsh_arg_ext_tar}
elif [ "$tsh_arg_compression_type" == "zip" ]; then
file_compression=${tsh_arg_ext_zip}
elif [ "$tsh_arg_compression_type" == "rar" ]; then
file_compression=${tsh_arg_ext_rar}
fi
if [ -z "${file_output}" ]; then
file_output="${file_target}"
fi
if [ -z "${file_upload}" ]; then
file_upload="${file_target}"
fi
# #
# Target > Is Folder
# #
local curl_code=0
local regex_IsUrl='(https?|ftp|file)://[-[:alnum:]\+&@#/%?=~_|!:,.;]*[-[:alnum:]\+&@#/%=~_|]'
if [ -d "$tsh_arg_target" ]; then
printf '%-29s %-65s\n' " ${c[yellow]}STATUS${c[end]}" "Target ${c[blue]}${file_target}${c[end]} is ${c[blue]}folder${c[end]}"
# #
# Upload Folder > Compress
#
# Compress the folder as a .tar.gz. We can't upload an entire folder
# #
printf '%-29s %-65s\n' " ${c[yellow]}STATUS${c[end]}" "Compressing target ${c[blue]}${file_target}${c[end]} to ${c[blue]}${file_output}.${file_compression}${c[end]} using ${c[blue]}$tsh_arg_compression_type${c[end]}"
if [ "${tsh_arg_compression_type}" == "tar" ]; then
tar czf "${file_output}.${file_compression}" "${file_target}/"
elif [ "${tsh_arg_compression_type}" == "zip" ]; then
# cd "${file_target}"
zip -qq -r "${file_output}.${file_compression}" "${file_target}/"
# cd -;
elif [ "${tsh_arg_compression_type}" == "rar" ]; then
rar a -ep1 -inul -r -rr10p "${file_output}.${file_compression}" "${file_target}/"
fi
file_output="${file_output}.${file_compression}"
file_upload="${file_upload}.${file_compression}"
# #
# Upload Folder > -e, --encrypt specified
#
# encrypt the specified folder with GPG
# #
if [ "$tsh_arg_encrypt" = true ]; then
file_upload="${file_output}.${tsh_arg_ext_gpg}"
printf '%-29s %-65s\n' " ${c[yellow]}STATUS${c[end]}" "Enabled ${c[blue]}GPG Encryption${c[end]}"
gpg --symmetric --batch --yes --armor --passphrase "${tsh_arg_encrypt_pass}" --output - "${file_output}" > "${file_upload}"
fi
# #
# Upload Folder > Curl
#
# upload processed folder to transfer.sh server
# #
printf '%-29s %-65s\n' " ${c[yellow]}STATUS${c[end]}" "Uploading file ${c[blue]}${file_upload}${c[end]} to ${c[blue]}${tsh_arg_uploadto}${c[end]}"
curl_output=$(curl --progress-ba --upload-file "./${file_upload}" "${tsh_arg_uploadto}/${file_upload}")
# #
# Upload Folder > Get Curl Output URL
# #
echo -e
if [[ "$curl_output" =~ $regex_IsUrl ]]; then
printf '%-27s %-65s\n' " ${c[green]}OK${c[end]}" "${c[end]}Successfully uploaded package ${c[green]}${file_upload}${c[end]} to ${c[green]}${curl_output}${c[end]}"
else
local curl_httpCode=$(echo "${curl_output}" | sed -e 's/.*\httpcode=//')
printf '%-29s %-65s\n' " ${c[red2]}ERROR${c[end]}" "${c[end]}Failed to upload package; curl error code ${c[red2]}${curl_httpCode}${c[end]}"
fi
echo -e
# #
# Target > Is File
# #
elif [ -f "$tsh_arg_target" ]; then
printf '%-29s %-65s\n' " ${c[yellow]}STATUS${c[end]}" "Target ${c[blue]}${tsh_arg_target}${c[end]} is ${c[blue]}file${c[end]}"
if [ ! -s "${file_target}" ]; then
echo -e
printf '%-29s %-65s\n' " ${c[red2]}ERROR${c[end]}" "${c[end]}Cannot Upload Blank File${c[end]}"
printf '%-29s %-65s\n' " ${c[yellow]}${c[end]}" "The specified file contains no data.${c[end]}"
printf '%-34s %-65s\n' " ${c[yellow]}${c[end]}" "${c[fuchsia2]}./$FUNCNAME ${c[grey2]}--target ${c[yellow]}\"folder|file\"${c[end]}"
printf '%-34s %-65s\n' " ${c[yellow]}${c[end]}" "${c[fuchsia2]}./$FUNCNAME ${c[grey2]}--target ${c[yellow]}\"folder|file\"${c[end]} ${c[grey2]}--encrypt${c[end]} ${c[grey2]}--password ${c[yellow]}\"PasswordHere\"${c[end]}"
echo -e
return
fi
local file_orig="${file_target}" # original filename (example.txt)
# #
# Upload File > -o, --output specified by user
#
# rename the target file to the name specified with --output
# at the end of the process; rename it back to its original name
# #
if [ "${tsh_arg_output}" = true ]; then
if [ "${file_target}" != "${file_output}" ]; then
printf '%-29s %-65s\n' " ${c[yellow]}STATUS${c[end]}" "Renaming ${c[blue]}${file_target}${c[end]} to ${c[blue]}${file_output}${c[end]}"
mv "${file_target}" "${file_output}"
fi
fi
# #
# Upload File > Compress
#
# Compress the file as a .tar.gz. We can't upload an entire folder
# #
if [ "$tsh_arg_compression_enabled" = true ]; then
printf '%-29s %-65s\n' " ${c[yellow]}STATUS${c[end]}" "Compressing target ${c[blue]}${file_target}${c[end]} to ${c[blue]}${file_output}.${file_compression}${c[end]} using ${c[blue]}$tsh_arg_compression_type${c[end]}"
if [ "${tsh_arg_compression_type}" == "tar" ]; then
tar czf "${file_output}.${file_compression}" "${file_target}"
elif [ "${tsh_arg_compression_type}" == "zip" ]; then
# cd "${file_target}"
zip -qq -r "${file_output}.${file_compression}" "${file_target}"
# cd -;
elif [ "${tsh_arg_compression_type}" == "rar" ]; then
rar a -ep1 -inul -r -rr10p "${file_output}.${file_compression}" "${file_target}"
fi
file_output="${file_output}.${file_compression}"
file_upload="${file_upload}.${file_compression}"
fi
# #
# Upload File > -e, --encrypt specified
#
# encrypt the specified file with GPG
# #
if [ "${tsh_arg_encrypt}" = true ]; then
file_upload="${file_output}.${tsh_arg_ext_gpg}"
printf '%-29s %-65s\n' " ${c[yellow]}STATUS${c[end]}" "Enabled ${c[blue]}GPG Encryption${c[end]}"
gpg --symmetric --batch --yes --armor --passphrase "${tsh_arg_encrypt_pass}" --output - "${file_output}" > "${file_upload}"
fi
# #
# Upload File > Curl
#
# upload processed folder to transfer.sh server
# #
printf '%-29s %-65s\n' " ${c[yellow]}STATUS${c[end]}" "Uploading file ${c[blue]}${file_upload}${c[end]} to ${c[blue]}${tsh_arg_uploadto}${c[end]}"
curl_output=$(curl --progress-ba --upload-file "./${file_upload}" "${tsh_arg_uploadto}/${file_upload}")
# #
# Upload File > -o, --output specified by user
#
# rename the file back to its original name if not already
# #
if [ "${tsh_arg_output}" = true ]; then
if [ "${file_output}" != "${file_orig}" ]; then
printf '%-29s %-65s\n' " ${c[yellow]}STATUS${c[end]}" "Renaming ${c[blue]}${file_output}${c[end]} to ${c[blue]}${file_orig}${c[end]}"
mv "${file_output}" "${file_orig}"
fi
fi
# #
# Upload File > Get Curl Output URL
# #
echo -e
if [[ "$curl_output" =~ $regex_IsUrl ]]; then
printf '%-27s %-65s\n' " ${c[green]}OK${c[end]}" "${c[end]}Successfully uploaded package ${c[green]}${file_upload}${c[end]} to ${c[green]}${curl_output}${c[end]}"
else
local curl_httpCode=$(echo "${curl_output}" | sed -e 's/.*\httpcode=//')
printf '%-29s %-65s\n' " ${c[red2]}ERROR${c[end]}" "${c[end]}Failed to upload package; curl error code ${c[red2]}${curl_httpCode}${c[end]}"
fi
echo -e
else
# #
# Target > Not file or folder
# #
echo -e
printf '%-29s %-65s\n' " ${c[red2]}ERROR${c[end]}" "${c[end]}No File or Folder Target Specified${c[end]}"
printf '%-29s %-65s\n' " ${c[yellow]}${c[end]}" "You must specify a valid target which is either a file or folder${c[end]}"
printf '%-34s %-65s\n' " ${c[yellow]}${c[end]}" "${c[fuchsia2]}./$FUNCNAME ${c[grey2]}--target ${c[yellow]}\"folder|file\"${c[end]}"
echo -e
return
fi
}
@Aetherinox
Copy link
Author

Aetherinox commented May 22, 2025

About

This is a Linux alias which allows for easier uploading to a transfer.sh server.


Notes

The following notes should be read.


Note

This alias provides three ways to encrypt:

  • tar
  • rar
  • zip

You must have the required packages installed on your system

sudo apt install rar unrar zip

  • Targeted folders automatically get compressed since you can't upload a folder as a single file.
  • Targeted files will not by compressed by default, must use -c, --compress option
  • Compression options are:
    • tar
    • rar
    • zip
  • The -e, --encrypt option uses GPG symmetric encryption; which requires a -p, --password to be specified, such as:
    • upload-sh --target Filename.ext --encrypt --password "YourPassword"
  • Show all commands and options available with upload-sh --help
  • This function contains multiple aliases to trigger the function:
    • file-upload --target File|Folder
    • upload-file --target File|Folder
    • sh-upload --target File|Folder
    • upload-sh --target File|Folder
    • transfer_upload --target File|Folder
  • Each option which starts with - or -- contains numerous aliases



Aliases

Each option that starts with - or -- has multiple names that it can be triggered with. The list below outlines the commands and their aliases.


Command Aliases Options
Version info -v, --ver, --version
Target file / folder -i, --in, -f, --file, -F, --folder, -t, --target
Encrypt file / folder -e, --encrypt, --gpg
Encrypt password -p, --pass, --password, --passphrase
Output file / folder name -o, --out, --output, --output-file
Compress -c, --compress
Compress Type -a, --archive, --archive-type, --compress-type, --type rar, tar, zip
Transfer.sh Server -s, --server, -u, --upload-to, --upload
Help menu -h, --help

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