Uploading/downloading small files across systems

Sometimes you just want to quickly move a copy of a script, image or binary from, for example, your local (linux) machine to another (linux) machine. The usual tool would be SCP, but this can get complicated when there are several layers of ssh and sometimes it doesn’t work at all (as is the case for transfers between the Department of Statistics computers and the outside world).

I have written a nice little script which makes use of hosting on file.io, as well as cURL, to upload files, and put a wget command into the clipboard which can be easily used to download it from any terminal window (and therefore on any system you can log in to). Folders will be compressed and the clipboard command will automatically unzip the result.

The only dependency is xclip, which can be installed on Ubuntu with:

sudo apt-get install xclip

If you put this somewhere on your path, call it upload_small_file.sh and change its permissions (chmod +x upload_small_file.sh) and add a symlink (ln -s upload_small_file.sh uag) then you can call it like so:

uag my_file.txt

So with all that said, here’s the script:

#!/usr/bin/bash
if [ -z "$1" ]; then
        echo "Please supply a file to upload"
        exit 1
fi
ARG="$1"
ISFOLDER=0
if [ -d "$ARG" ]; then
        ARG=$ARG.tar.gz
        echo "Argument passed is a folder; creating temporary archive $ARG"
        tar -cvf $ARG $1
        ISFOLDER=1
fi

FNAME=$(basename -- $ARG)
OUTPUT=$(curl -F "file=@$ARG" -s -w "\n" https://file.io)
SUCCESS=$(echo "$OUTPUT" | awk -F '"' '{print $2}')

if [ $SUCCESS = "success" ]; then
        LINK=$(echo "$OUTPUT" | awk -F '"' '{print $10}')
        if [ $ISFOLDER = 1 ]; then      
                echo "wget -O $FNAME $LINK; tar -xvf $ARG; rm -rf $ARG" | xclip
        else
                echo "wget -O $FNAME $LINK" | xclip
        fi
        echo "Sucessfully uploaded to $LINK which has been copied to clipboard"
        if [ $ISFOLDER = 1 ]; then
                echo "Deleting temporary archive $ARG..."
                rm -rf $ARG
                echo "Deleted."
        fi
else
        echo "There was a problem uploading this file:"
        echo $OUTPUT
fi

The link (e.g. wget <generated_link> -O myfile.txt) will be in your middle mouse wheel, to be used in whichever terminal you want to download the file to.

Author