• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1076
  • Last Modified:

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
0
mruff
Asked:
mruff
  • 4
  • 4
2 Solutions
 
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now