Link to home
Start Free TrialLog in
Avatar of mruff
mruff

asked on

sFTP unix create remote folder structure

Dear experts,
I have to upload files to a remote server to various directories.
Now I want to ensure that the remote directory structure is existing and correct. It is NOT the same as the source local directory structure, I therefore cannot use rsynch.
What I did: In my unix shell script I created myself a command file to be executed on the remote server:
cd /rootsFTPremotePath
mkdir A
cd A
mkdir B
This works fine now I have another upload script which generates the following remote commands:
cd /rootsFTPremotePath
mkdir A
cd A
mkdir C
->The problem is that mkdir A fails since the directory already exists, so directory C does NOT get created
I want a solution where the command mdkir A does NOT break if it already exists.
The most simple solution
I have several shell scripts which creates a destination folders creation scripts

So bottom line I just want the easiest solution in order to create missing destination folder in the context where script number one creates
mkdir A
cd A
mkdir B

script number two
mkdir A
cd A
mkdir C
->ensure that script 2 creates subolder C of A->Prevent script number 2 of failing at the command mkdir A and exiting
->Botttom line: Execute the whole script even there is an error at command number xy
Thank you
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Easiest way is probably using the "-p" ("parent") flag of "mkdir".

This flag creates the whole tree in one go, and

mkdir -p A/C

will not fail if A or even A/C already exist, but will create both or either or none, depending on what's already there.

The above is not restricted to just one level, so

mkdir -p A/B/C/D

will create all non-existing directories and will not complain about already existing ones.
Avatar of mruff
mruff

ASKER

Hi
Unfortunately this is not working
I tried both ways:
mkdir -p /Alreadyexisting/NewA/NewB
->Couldn't create directory: Failure
cd /Alreadyexisting/
mkdir -p NewA/NewB
->Couldn't create directory: Failure
The posted results don't look like "Alredy existing" errors from the Linux command "mkdir". You should have seen "File exists" or the like instead of "Failure".

Are you really in a Linux shell environment? Or are we talking about the SFTP subcommand "mkdir"? This one does not support "-p"!

If you mean the subcommand then you should now have a directory "-p" on the target server.

The drawback with SFTP is that it doesn't support any logic (scripting).
You would have to run a separate bash command via ssh on the remote host (or perhaps even a bash/ksh script) to achieve your goal.
Avatar of mruff

ASKER

Ah, got it, I was performing the -p when connected via sFTP to the remote server
So how do I created this remote directory structure on the remote server?
As of now I was login in with sFTP
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mruff

ASKER

Unfortunately the user is restricted ....
Avatar of mruff

ASKER

is there a easy way to check on the remote server whether a folder exists?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial