How To Upload & Share Files From The Linux Terminal With Transfer.sh

These days, sharing files is easier than ever. Between Dropbox, Google Drive, OneDrive etc., getting things from one place to another works quite fast — unless you’re using the terminal. If you are using the Terminal, chances are you’re still getting files over SFTP, or something equally as archaic. Introducing TransferSH: a file sharing tool specifically designed to let you upload & share files from the Linux Terminal.

Using Transfer.sh Manually

Transfer.sh has a web interface and you can upload and share files from your browser. The service also works with the command line so you can share a file directly through the terminal. To share files from the Linux terminal using Transfer.sh, you need Curl.

Ubuntu

sudo apt install curl

Debian

sudo apt-get install curl

Arch Linux

sudo pacman -S curl

Fedora

sudo dnf install curl

OpenSUSE

sudo zypper install curl

Other Linuxes

Users of non-mainstream Linux distributions shouldn’t have too much trouble finding the Curl program, due to how widely available it is. To get it, simply open up your package manager, search for “curl” and install it! If for some reason it is not available for you, download it here.

Using Curl To Upload

Interacting with transfer.sh using curl requires quite a long command. Here’s how it works.

First, find a file on your file-system to upload. Use “find” or “locate” in your terminal (or go to the file manager, find the file and take note of where it is). After taking note of where the file you’re trying to upload is, use the cd command to go there. In this example, we’ll be uploading “test.mp3” from the Documents folder.

cd ~/Documents

The terminal is now in the directory where test.mp3 is. Now, use this command “formula” to upload the file. This is the first part of the command:

 curl --upload-file ./test.mp3

The next step is to create a URL for transfer.sh to use. Keep in mind the file name (as well as the extension) and write it like this:

https://transfer.sh/test.mp3

The end result should look like this;

curl --upload-file ./test.mp3 https://transfer.sh/test.mp3

Curl will send the file to Transfer.sh, and return a URL. Open the URL to download the file, or send it to whomever you wish to share the file with.

Transfer.sh Terminal Alias

Uploading manually with Curl can get you out of a tight spot, but it’s not perfect. There’s no indication that the upload is working, how much of a file has been uploaded, and remembering a sentence just to upload something is quite tedious. It is because of this, the developer made a Bash alias. This bash alias, when set up will give the user the ability to easily upload files and folders, with a single word.

To set it up, open your BashRC file. Understand that each individual user’s BashRC file must be modified to use this alias.

nano ~/.bashrc

Using Nano, go to the very end of the file. Then, paste the Transfer SH Alias code. Be sure to paste each and every bit of the code, or the bash alias will not work.

Note: pasting using Nano via the clipboard can be done with CTRL + SHIFT + V.

transfer() {
 # check arguments
 if [ $# -eq 0 ];
 then
 echo "No arguments specified. Usage:necho transfer /tmp/test.mdncat /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
 echo ""
 # cleanup
 rm -f $tmpfile
 }

After pasting the long amount of code to the ~/.bashrc file, save nano with CTRL + O. Restart your PC so that the edits will go into effect.

Using Terminal Alias To Upload

Uploading with the TransferSH alias is much easier than using Curl manually. In this example, we’ll again use test.mp3 and the Documents folder.

Step 1: navigate the terminal to where the file/folder you wish to upload is at.

cd ~/Documents

Step 2: start the upload process.

transfer test.mp3

or, for a folder do:

transfer file_folder

Step 3: Wait for the terminal to upload the file. A progress bar will go across the screen, and it will spit out a download link when complete.

Downloading with Curl

Curl is more than just an upload tool. It’s a download tool too. To download a file uploaded by transfer.sh, follow these instructions.

First, copy the download link that the upload tool gives you and keep it in your clipboard. Then, do this to download:

curl https://transfer.sh/vN79X/test.zip > test.zip

Be sure to re-write the name of the file after > to the one in the download link. This is what curl names the download on your PC. It’s possible to change the name, but we do not recommend it.

Downloading With Wget

Despite how popular Curl is, some people refuse to use it as it comes off as complicated, and instead choose to use the Wget download too. It makes sense, as Wget is very straightforward. Like the Curl tool, Wget can also easily download Transfer SH links. Here’s how:

wget https://transfer.sh/vN79X/test.zip

Conclusion

It doesn’t happen often, but at times while working on a server you may need quick access to a file from another computer. Setting up SSH, logging into FTP etc., are tedious. That’s why TransferSH is such a useful tool. It makes it so that grabbing files of any size right from the command line easier than ever.

Source