Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

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?

did you try googling this? I get a number of results, including: this and this . In other words, are you using the latest version of lftp ? Matthias Aug 25, 2016 at 11:47 MyFault Aug 25, 2016 at 11:51 yeah I can't seem to find an answer there either. But there is a newer version of LFTP, you could try to upgrade and see if that helps. Matthias Aug 25, 2016 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, 2016 at 11:58 tried -f on lftp v4.7.4 , it fails silently. We are using lftp for auto-deploy (not my decision), and have more commands after it, but deployer does not proceed to the next command even with -f . Nuryagdy Mustapayev Sep 23, 2021 at 15:02

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.

Looks like mkdir -pf /my/new/directory || cd /my/new/directory is a solution for me: no error messages, no error codes, unless there is something wrong with the directory (permissions, the name exists as a file etc.)

UPD: In my case I only needed to create directory. If the command used in a longer script, additional cd command might be needed, since the working directory after the command above depends on whether /my/new/directory existed before.

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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.