Need to create automated Directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to create automated Directories
# 1  
Old 07-24-2014
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

Code:
 
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 !!
# 2  
Old 07-24-2014
Assuming you have a secure key exchange in place
Code:
find /opt/app/Output -type d -exec ssh $REMOTE_HOST mkdir -p {} \;

# 3  
Old 07-24-2014
Quote:
Originally Posted by Skrynesaver
Assuming you have a secure key exchange in place
Code:
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 ?
# 4  
Old 07-24-2014
An ugly solution ...
Code:
find /opt/app/Output -type d | perl -ne '$_=~s{^/opt/app/}{/tmp/mydir/};system(ssh "$REMOTE_HOST "mkdir -p $_");' -

# 5  
Old 07-24-2014
Probably a neater solution:-
Code:
cd /opt/app/Output
find . -type d -exec ssh $REMOTE_HOST "cd /tmp/mydir ; mkdir -p {}" \;

Untested though.... Smilie



I hope that this helps,
Robin
# 6  
Old 07-24-2014
Wouldn't these run ssh and login for every single (sub-) directory found? Not sure the closing "+" instead "\;" could remedy that.
# 7  
Old 07-24-2014
If the number of directories to make and therefore the number of SSH connections to make becomes a problem, then you could do this:-
Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create automated scan of specific directory using bash

I am trying to use bash to automate the scan of a specific directory using clamav. Having this in place is a network requirement. The below is an attempt to: 1. count the extensions (.txt, .jpeg) in a directory and write them to a virus-scan.log (section in bold) 2. scan each folder in the... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. UNIX for Dummies Questions & Answers

Create 2 directories in one command

Hi how can i create 2 directories in two different directories ($HOME and $PWD) with 1 command? dir 1 in $HOME and dir2 in $PWD (2 Replies)
Discussion started by: chinababy
2 Replies

3. Ubuntu

Create a customized ubunto automated installtion cd

Hi 1- I want to create a cd or dvd of ubunto , that include a lot of installed packages entered by me . 2- automated installion (just enter the cd )and install by it self . (3 Replies)
Discussion started by: rashed
3 Replies

4. Shell Programming and Scripting

Write an automated shell program(s) that can create, monitor the log files and report the issues for

Hi , Please help me getting this done. Write an automated shell program(s) that can create, monitor the log files and report the issues for matching pattern. (i) Conditions for creating log files. Log file is created with date (example 2010_03_27.log). If the log file size is 10 Mb for... (1 Reply)
Discussion started by: itian2010
1 Replies

5. Shell Programming and Scripting

check if multiple directories exist else create missing directories

Hi , I 'm trying to check if multiple directories exist on a server, if not create the missing ones and print " creating missing directory. how to write this in a simple script, I have made my code complex if ; then taskStatus="Schema extract directory exists, checking if SQL,Count and... (7 Replies)
Discussion started by: ramky79
7 Replies

6. UNIX for Dummies Questions & Answers

Create zip without including directories

Hi guys, I'm trying to do the following: zip -r /tmp/foo.zip public/accounts/foo But the zip that's been made has the whole "public/accounts/foo" path. I want only the foo folder to be zipped. How can I do this? Thanks, Elías (2 Replies)
Discussion started by: elioncho
2 Replies

7. UNIX for Dummies Questions & Answers

Want to create 3 different new directories under the same path

Hi, Iam new to UNIX...My requirement is to create 3 dir as an hierarchy under /var/opt/temip.The output should be /var/opt/temip/GP_Int/GPTTS/AUTO. I have tried the following script...But only GP_int folder is getting created and not other folders...Can someone help??? #!/usr/bin/ksh #script... (1 Reply)
Discussion started by: Llb
1 Replies

8. UNIX for Dummies Questions & Answers

How to create shotcuts to the directories

Hi, I need your help in writing shortcuts to my directories. So that I can go into the directories with the help of shortcuts. For example: there is a directory called /home/java/webapps/project1 I want to give a shortcut as project1 . So whenever I have give cd project 1 from command line ... (3 Replies)
Discussion started by: TonySolarisAdmi
3 Replies

9. UNIX for Dummies Questions & Answers

Create directories with regular expression

Hi guys, can any one tell me how to create directories using regular expression? Let's say that I need to create directories test01, test02, test03.... test10. Can it be done using any regular expression? thanks. (13 Replies)
Discussion started by: mahendrt
13 Replies

10. UNIX for Dummies Questions & Answers

How to create directories

Hi... Can any1 help me by telling me the way to create multiple directories using single command.... to create 1 directory.. mkdir is used.... :D but how to create multiple direcs. like 4 direc. i tried .... $ mkdir a; mkdir b; mkdir c; mkdir d But its 4 commands in a single... (3 Replies)
Discussion started by: abishekmag
3 Replies
Login or Register to Ask a Question