Skip to content

Instantly share code, notes, and snippets.

@winuxue
Last active March 20, 2018 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save winuxue/80afc32c160272ba3ba7bdd932dc8aee to your computer and use it in GitHub Desktop.
Save winuxue/80afc32c160272ba3ba7bdd932dc8aee to your computer and use it in GitHub Desktop.

transfer.sh - bash script

Defines an alias to share file(s) and folder using transfer.sh

Installation

mkdir transfer-sh

cd transfer-sh

git clone https://gist.github.com/80afc32c160272ba3ba7bdd932dc8aee.git .

cat transfer.sh >> ~/.bashrc

. ~/.bashrc

optional requirements

  • xclip: allows copy the generated link to your clipboard.

Usage

  • for single file
transfer document.pdf
  • for two or more files
transfer document1.pdf document2.pdf
  • for files with spaces in it's name
transfer "document 1.pdf"
  • for directories
transfer mydir
################################################################################################
#
# Defines transfer alias and provides an easy command line for file(s) and folder sharing.
#
################################################################################################
transfer() {
# check curl
curl --version 2>&1 > /dev/null
if [ $? -ne 0 ]; then
echo "Could not find curl."
return 1
fi
# check arguments
if [ $# -ne 1 ];
then
echo -e "Wrong arguments specified. Usage:\ntransfer /tmp/test.md\ncat /tmp/test.md | transfer test.md"
return 1
fi
# read stdin or file
arg="$1"
#check if path exists
if [ ! -e "$arg" ];
then
echo "$arg doesn't exists."
return 1
fi
# get 'slug' and 'file' name
if tty -s;
then
slug="$( basename "$arg" | sed -e 's/[^a-zA-Z0-9._-]/-/g' )"
if [ -d "$arg" ];
then
# zip directory
zipfile="$(mktemp -u -t transferXXX.zip)"
echo "zipping..."
zip "$zipfile" -r -q "$(basename "$arg")"
file="$zipfile"
else
file="$arg"
fi
else
slug="$arg"
file="-"
fi
# get temporary filename, output is written to this file so show progress can be showed
tmpfile="$( mktemp -t transferXXX )"
# transfer file or pipe
echo "uploading..."
curl --progress-bar --upload-file "$file" "https://transfer.sh/$slug" >> "$tmpfile"
# copy link to the clipboard
if [ -x "$(command -v xclip)" ];
then
xclip -sel clip < "$tmpfile"
fi
# cat output link
cat "$tmpfile"
echo
# cleanup
rm -f "$tmpfile"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment