Save file to your Nextcloud from bash

By | 2019-03-12

Sometimes you need to upload from command line to you Nextcloud storage. You can do it with a simple curl command.

At first you have to create a shared directory in your Nextcloud with “Allow upload and editing” share option.

You will get a share link for example: https://nextcloud.myserver.com/index.php/s/gz8BS7mtxGbwkQB

The string “gz8BS7mtxGbwkQB” will be used as username.

Insert the following code to your .bashrc:

function save_to_nextcloud {
     file=$1
     if test -z "$file" -a -r "$file" -a -f "$file"
     then
         echo "A filename is needed and the file should be readable!"
         return
     fi
     /usr/bin/curl -k -T $file -u "gz8BS7mtxGbwkQB:" -H 'X-Requested-With: XMLHttpRequest' https://nextcloud.myserver.com/public.php/webdav/$file
}

Replace the user gz8BS7mtxGbwkQB to your own userid. Beware! The “:” at the end of username is needed due the “-u” option of curl.

In a new bash instance you can save the given file to your shared Nextcloud directory with the “save_to_nextcloud” function.

For example:

~$ save_to_nextcloud testfile.txt