Need to create automated Directories

Hi,

On serverB i wish to have a script that creates ONLY & EXACTLY the same folder structure that i provide on ServerA.

Thus if serverA has a folder "Output" under /opt/app/Output and has the below folders under Output

 
Output
Output/logs
Output/reciever
Output/data
Output/reciever/tmp
...
...

I need the similar folder structure created on serverB. What should be the easiest way to achieve this ?

Just the folders not the files !!

Assuming you have a secure key exchange in place

find /opt/app/Output -type d -exec ssh $REMOTE_HOST mkdir -p {} \;

How can we specify the remote host directory ... if i wish to create the Output folder in /tmp/mydir of serverB ?

An ugly solution ...

find /opt/app/Output -type d | perl -ne '$_=~s{^/opt/app/}{/tmp/mydir/};system(ssh "$REMOTE_HOST "mkdir -p $_");' -

Probably a neater solution:-

cd /opt/app/Output
find . -type d -exec ssh $REMOTE_HOST "cd /tmp/mydir ; mkdir -p {}" \;

Untested though.... :rolleyes:

I hope that this helps,
Robin

Wouldn't these run ssh and login for every single (sub-) directory found? Not sure the closing "+" instead "\;" could remedy that.

If the number of directories to make and therefore the number of SSH connections to make becomes a problem, then you could do this:-

cd /opt/app/Output

printf "cd /tmp/mydir" > /tmp/mkdir_cmds

find . -type d -exec printf "mkdir -p {} \n" \; >> /tmp/mkdir_cmds

sftp $REMOTE_HOST <<EOSFTP
put /tmp/mkdir_cmds
EOSFTP

ssh  $REMOTE_HOST "chmod 700 /tmp/mkdir_cmds ; /tmp/mkdir_cmds"

This will put all the commands into a file, send it and then execute it.

It's more to code (and I haven't checked it for errors) so unless creating so many SSH connections in a loop is a problem, I'd stick with the earlier suggestions.

Robin

How about

origin=/tmp/mydir
find * -type d -printf "mkdir -p $origin/%p\n" | ssh -qt user@host 

Error checking etc. to be added...

EDIT: or even

{ echo cd /tmp; find * -type d -printf "mkdir %p\n"; } | ssh -qt -2p2200 root@80.131.44.87 

What about directory owner/permissions?

Perhaps rsync will get you there easier:

rsync -av --include='*/' --exclude='*' /opt/app/Output/ ServerB:/opt/app/Output/
1 Like

I tried this code but the command never seems to complete. This is what i see and it remain like this forever.

sending incremental file list

On the other server, yes I see the folder structure created but i am not sure if it is complete folder structure or if it is still getting updated.

cksum on the copied folder gives me the below error msg.

$ cksum FOLDER-Out
4294967295      0       FOLDER-Out [read error]

Anything wrong ?

You can't run cksum on a folder, it reads files.

What does top show when rsync is running?

Try find folders -type f to see if rsync was creating any files in there.

You could add the --progress and --stats options for a bit more viability on what is going on:

rsync -av --include='*/' --exclude='*' --progress --stats /opt/app/Output/ ServerB:/opt/app/Output/
1 Like