Create a gist now

Instantly share code, notes, and snippets.

Bash and zsh alias for transfer.sh. Transfers files and directories to transfer.sh.
function transfer
if test (count $argv) -eq 0
echo "No arguments specified. Usage:\necho transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md"
return 1
end
## get temporarily filename, output is written to this file show progress can be showed
set tmpfile ( mktemp -t transferXXX )
## upload stdin or file
set file $argv[1]
#if tty -s;
#then
set basefile (basename "$file" | sed -e 's/[^a-zA-Z0-9._-]/-/g')
# if [ ! -e $file ];
# then
# echo "File $file doesn't exists."
# return 1
# fi
if test -d $file
# zip directory and transfer
set zipfile ( mktemp -t transferXXX.zip )
# echo (dirname $file)
#cd (dirname $file) and echo (pwd)
zip -r -q - $file >> $zipfile
curl --progress-bar --upload-file "$zipfile" "https://transfer.sh/$basefile.zip" >> $tmpfile
rm -f $zipfile
else
# transfer file
curl --progress-bar --upload-file "$file" "https://transfer.sh/$basefile" >> $tmpfile
end
#else
# # transfer pipe
# curl --progress-bar --upload-file "-" "https://transfer.sh/$file" >> $tmpfile
#fi
## cat output link
cat $tmpfile
## cleanup
rm -f $tmpfile
end
#
# Defines transfer alias and provides easy command line file and folder sharing.
#
# Authors:
# Remco Verhoef <remco@dutchcoders.io>
#
curl --version 2>&1 > /dev/null
if [ $? -ne 0 ]; then
echo "Could not find curl."
return 1
fi
transfer() {
# check arguments
if [ $# -eq 0 ];
then
echo "No arguments specified. Usage:\necho transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md"
return 1
fi
# get temporarily filename, output is written to this file show progress can be showed
tmpfile=$( mktemp -t transferXXX )
# upload stdin or file
file=$1
if tty -s;
then
basefile=$(basename "$file" | sed -e 's/[^a-zA-Z0-9._-]/-/g')
if [ ! -e $file ];
then
echo "File $file doesn't exists."
return 1
fi
if [ -d $file ];
then
# zip directory and transfer
zipfile=$( mktemp -t transferXXX.zip )
cd $(dirname $file) && zip -r -q - $(basename $file) >> $zipfile
curl --progress-bar --upload-file "$zipfile" "https://transfer.sh/$basefile.zip" >> $tmpfile
rm -f $zipfile
else
# transfer file
curl --progress-bar --upload-file "$file" "https://transfer.sh/$basefile" >> $tmpfile
fi
else
# transfer pipe
curl --progress-bar --upload-file "-" "https://transfer.sh/$file" >> $tmpfile
fi
# cat output link
cat $tmpfile
# cleanup
rm -f $tmpfile
}
@Mike-T-Mitchell

Thank you so much for taking the time to lay this out as clearly as possible. This functionality is awesome, and I definitely look forward to using it on a day-to-day basis!

@nl5887
Owner
nl5887 commented Dec 18, 2014

Thanks! Updated it to upload folders as well.

@orschiro
orschiro commented Jan 5, 2015

This alias function works great with Zsh. Many thanks!

@Nygaardc

I get the following error on both my RPI2 running Raspbian as well as on my Mac running OS X Yosemite:
bash: ((: ! $+commands[curl] : syntax error: operand expected (error token is "$+commands[curl] ").
Seems like it has something to do with the first IF statement so I just removed that (which helped) but still thought you might want to know about this.

@kyrias
kyrias commented Jun 17, 2015

$+commands[curl] is a zsh thing and doesn't work in Bash.

@nl5887
Owner
nl5887 commented Jul 18, 2015

Thx, fixed it.

@jozsi
jozsi commented Aug 3, 2015

On Mac OS X Yosemite the bash script will always create a file named 1.

I beleive line 8 should be:

curl --version 2>&1 > /dev/null
@nl5887
Owner
nl5887 commented Oct 19, 2015

thx, fixed

@GochoMugo

Thanks for this! Already used here.

@bitbonsai

Hi, I added a line on the bash version, to copy the link to clipboard...
If I just pipe it without the extra line, it doesn't show on terminal, so DRY suffered a bit.
Awesome product, thanks!

cat $tmpfile | pbcopy
cat $tmpfile
@rockymadden

@bitbonsai, nice idea! Remove the newline at the end: cat $tmpfile | tr -d '\n' | pbcopy

@jackmasa

Is there a version of.Bat?

@priezz
priezz commented Aug 11, 2016

Nice. However quotes are missing around $file (4 occurrences).

@jyoo
jyoo commented Aug 31, 2016 edited

need to add a "-e" to echo for bash to interpret the newlines "\n".
Also looks like there's a redundant 'echo' in there?

Thank you so much for this service. You are beyond awesome. Wish I found out about it sooner!

Update: You can see my suggestions in my fork: https://gist.github.com/nl5887/a511f172d3fb3cd0e42d

@melmi
melmi commented Oct 1, 2016

I am using an older version of this alias. I tried to upload a file which its name contained brackets. I got an error because brackets have some special meaning for curl, apparently. So I suggest calling curl with -g switch to make it treat brackets in filenames as normal characters.

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