bash LFTP - 如果目录不存在则创建目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39144209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:04:59  来源:igfitidea点击:

LFTP - Create directory if it does not exist

linuxbashlftp

提问by MyFault

I would like to use LFTP to create a directory if it does not exist. It should be a "one-liner":

如果目录不存在,我想使用 LFTP 创建一个目录。它应该是一个“单行”:

This does already work:

这确实有效:

lftp -c "open -u user,pass server; mkdir /test

lftp -c "open -u user,pass server; mkdir /test

The

lftp -c "open -u user,pass server; mkdir -p /test

lftp -c "open -u user,pass server; mkdir -p /test

fails if the directory already exists:

如果目录已经存在,则失败:

mkdir: Zugriff nicht m?glich:550-Can't create directory: File exists 16 files used (0%) - authorized: 50000 files 1286621 Kbytes used (0%) - authorized: 512000000 Kb (/test2)

mkdir: Zugriff nicht m?glich:550-无法创建目录: 文件存在 使用了 16 个文件 (0%) - 授权: 50000 个文件使用了 1286621 KB (0%) - 授权: 512000000 Kb (/test2)

But it does fail if the directory does already exist. How can I do this more elegant?

但是如果目录已经存在,它就会失败。我怎样才能做得更优雅?

回答by lav

You can use mkdir -foption to suppress the error message. The option is available starting with 4.5.2 version. The latest lftp version is 4.7.3.

您可以使用mkdir -f选项来抑制错误消息。该选项从 4.5.2 版本开始可用。最新的 lftp 版本是 4.7.3。

回答by Julien Lirochon

If you can't upgrade to the latest version of lftp, you can use this :

如果你不能升级到最新版本的 lftp,你可以使用这个:

lftp -c "cd /my/new/directory || mkdir -p /my/new/directory"

It will create the directory only if it can't enter it.

只有当它不能进入​​目录时,它才会创建目录。

回答by Matthias

Do you mean simply mkdir -p /test?

你的意思是简单mkdir -p /test吗?

Answered here: How to mkdir only if a dir does not already exist?

在这里回答:仅当目录不存在时如何 mkdir?

From the mkdir --help:

来自mkdir --help

-p, --parents no error if existing, make parent directories as needed

-p, --parents 如果存在没有错误,根据需要创建父目录