1

I want to upload files to my nextcloud server. The problem is that I got an error. The first curl command should create the directory.

curl -u "$USER":"$PW" -X MKCOL "https://MYSERVER/remote.php/dav/files/$USER/$MANY_DIRECTORIES"
curl -u "$USER":"$PW" -T "$FILE" "https://MYSERVER/remote.php/dav/files/$USER/$MANY_DIRECTORIES/$FILE"

If $MANY_DIRECTORIES contains just one directory it works. But if this variable contains e.g. /root/deep/deeper and deep doesn't exist I got this error:

<?xml version="1.0" encoding="utf-8"?>
<d:error xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">
  <s:exception>Sabre\DAV\Exception\Conflict</s:exception>
  <s:message>Parent node does not exist</s:message>
</d:error>

The second command throws this error:

<?xml version="1.0" encoding="utf-8"?>
<d:error xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">
  <s:exception>Sabre\DAV\Exception\NotFound</s:exception>
  <s:message>File with name //test could not be located</s:message>
</d:error>

So how can I create the directories recursive to upload the file?

Thanks.

Mathias Conradt
  • 28,420
  • 21
  • 138
  • 192
cy221
  • 1,029
  • 3
  • 12
  • 24

3 Answers3

5

There is no option to create recursive directories, so I split the variable into an array and create the directories one by one.

IFS='/' read -r -a array <<<"$2"
for el in "${array[@]}"
do
        TEMP=$TEMP/$el
        curl -u "$USER:$PW" \
        -X MKCOL \
        "https://MYSERVER/remote.php/dav/files/$USER$TEMP"
done

curl -u "$USER:$PW" \
         -T "$1" "https://MYSERVER/remote.php/dav/files/$USER/$2/$1"
cy221
  • 1,029
  • 3
  • 12
  • 24
0

In cy221's answer I would replace the first lines with

tree=$(find .  -mindepth 1 -type d | sed 's!^./!!')
for el in $tree; do 
    #etc.
done

This will of course not work for directories with spaces in it. (sorry for using an answer instead of a comment, I couldn't get code formatting to work in a 'mere' comment).

plijnzaad
  • 313
  • 3
  • 11
-1

There's no method for that behaviour!

APROACH:

You need to create your own recursive function, that iterate over the directories path and create them one by one, like cy221 ilustrate in the answer above.

IgniteCoders
  • 4,834
  • 3
  • 44
  • 62