7

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

This does already work:

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

The

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)

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

MyFault
  • 407
  • 5
  • 18
  • did you try googling this? I get a number of results, including: [this](https://github.com/welaika/wordmove/issues/103) and [this](http://stackoverflow.com/questions/4171236/lftp-mkdir-p-doesnt-work-with-sftp-protocol). In other words, are you using the latest version of `lftp`? – Matthias Aug 25 '16 at 11:47
  • Yes, I tried to. LFTP is `LFTP | Version 4.0.9 | Copyright (c) 1996-2010 Alexander V. Lukyanov` which is installed from the repo. – MyFault Aug 25 '16 at 11:51
  • yeah I can't seem to find an answer there either. But there [is](https://lftp.yar.ru/news.html) a newer version of LFTP, you could try to upgrade and see if that helps. – Matthias Aug 25 '16 at 11:54
  • still wouldn't get my hopes up, from the same page as in my comment above: Version 3.7.12 - 2009-04-28 fixed core dump on `mput -d' command. fixed a core dump on `kill' command. fixed mkdir -p for sftp protocol. fixed some signed/unsigned conversion bugs. – Matthias Aug 25 '16 at 11:58

3 Answers3

7

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

lav
  • 1,177
  • 8
  • 16
4

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

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

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

Julien Lirochon
  • 731
  • 6
  • 8
-1

Do you mean simply mkdir -p /test?

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

From the mkdir --help:

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

Community
  • 1
  • 1
Matthias
  • 1,887
  • 1
  • 18
  • 35