Reputation: 427
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?
Upvotes: 8
Views: 13758
Reputation: 475
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.
Upvotes: 0
Reputation: 751
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.
Upvotes: 4
Reputation: 1481
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.
Upvotes: 10
Reputation: 3900
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
Upvotes: -1