如何在curl或WebDav中递归创建目录?

问题描述 投票:0回答:3

我想将文件上传到我的 nextcloud 服务器。问题是我遇到了一个错误。第一个curl命令应该创建目录。

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"

如果

$MANY_DIRECTORIES
仅包含一个目录,则它可以工作。但是如果这个变量包含例如
/root/deep/deeper
deep
不存在我收到此错误:

<?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>

第二个命令抛出此错误:

<?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>

那么如何递归创建目录来上传文件呢?

谢谢。

curl recursion webdav nextcloud
3个回答
5
投票

没有创建递归目录的选项,所以我将变量拆分为一个数组,然后逐个创建目录。

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"

0
投票

在 cy221 的回答中,我会将第一行替换为

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

这当然不适用于其中包含空格的目录。 (抱歉使用答案而不是评论,我无法在“纯粹”评论中使用代码格式)。


-3
投票

这种行为没有办法!

接近:

您需要创建自己的递归函数,迭代目录路径并一一创建它们,如上面answer中的cy221所示。

© www.soinside.com 2019 - 2024. All rights reserved.