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
mruffAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
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.
0
mruffAuthor Commented:
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
0
woolmilkporcCommented:
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.
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

mruffAuthor Commented:
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
0
woolmilkporcCommented:
Before logging in with sftp run

ssh user@remoteserver "mkdir -p A/B/C/D"

Then log in with sftp:

sftp user@remoteserver ... ...

I really hope that your userid is not restricted to using SFTP only!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mruffAuthor Commented:
Unfortunately the user is restricted ....
0
mruffAuthor Commented:
is there a easy way to check on the remote server whether a folder exists?
0
woolmilkporcCommented:
As I wrote, SFTP does not support scripting.

We could try an ugly trick to check for existing directories:

if echo $(sftp user@remotehost 2>&1 <<EOF
mkdir A
EOF
)  | grep -q "Failure"; then
  echo "Directory exists"
    else
      echo "Directory created"
fi

Instead of just echoing failure/success messages you could of course perform any other action.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.