LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-27-2013, 04:15 AM   #1
kumarjohn
LQ Newbie
 
Registered: Oct 2012
Posts: 22

Rep: Reputation: Disabled
script to create same directory stucture


Hi,

I want to write a script which will create the same directory structure on the other server.

one of the directory has many sub directories and few sub directories have some more sub directories in it.

I want to have exact directory structure without any files.

Best Regards,
KJ.
 
Old 02-27-2013, 05:05 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Just one command:
Code:
rsync -av --include '*/' --exclude '*' /path/to/dir user@host:
this copies the whole directory structure under /path/to/dir (dir included) to the remote machine. As you can see it excludes all and includes directories only.
 
2 members found this post helpful.
Old 02-27-2013, 05:54 AM   #3
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
This may help you in creating empty directories.
[user@machine ~]$ mkdir -p a/b/c/d/e/f/g/h
[user@machine ~]$ tree a
a
`-- b
`-- c
`-- d
`-- e
`-- f
`-- g
`-- h
[user@machine ~]$ mkdir -p a/{b/{b1,b2,b3},c,d}/e/f/g/h
[user@machine ~]$ tree a
a
|-- b
| |-- b1
| | `-- e
| | `-- f
| | `-- g
| | `-- h
| |-- b2
| | `-- e
| | `-- f
| | `-- g
| | `-- h
| |-- b3
| | `-- e
| | `-- f
| | `-- g
| | `-- h
| `-- c
| `-- d
| `-- e
| `-- f
| `-- g
| `-- h
|-- c
| `-- e
| `-- f
| `-- g
| `-- h
`-- d
`-- e
`-- f
`-- g
`-- h

32 directories, 0 files


Give it a try. you can figure out how does "mkdir -p" works.
tree command is used display the directory structure.
 
Old 02-27-2013, 08:52 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
While rsync obviously is way less work, if you want to try 'mkdir' then at least do something like
Code:
ssh user@destination; cd /target/directory; ssh user@source 'cd /source/directory; find ./* -type d'|xargs -iX mkdir -p 'X'
 
1 members found this post helpful.
Old 02-28-2013, 02:45 AM   #5
kumarjohn
LQ Newbie
 
Registered: Oct 2012
Posts: 22

Original Poster
Rep: Reputation: Disabled
Thumbs up

Hi,

Thanks for the replies.

rsync worked out any issues.

Quote:
Originally Posted by colucix View Post
Just one command:
Code:
rsync -av --include '*/' --exclude '*' /path/to/dir user@host:
Thank you colucix for the solution.

Unspawn, your solution worked similar to rsync. combination of multiple commands was nice.

RaviTezu thanks for replying and pointing to -p option in mkdir. It shall be tough for me to compare each and every sub directories and create the same structure as number of sub directories is huge and i can commit errors while creating also.

Best Wishes,
KJ.

Last edited by kumarjohn; 02-28-2013 at 02:50 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Script to create new directory with date moyorakkhi Linux - Newbie 3 04-22-2011 04:14 AM
script to create directory with date thetiger2003 Linux - Newbie 3 12-06-2010 05:40 AM
Create a script to cd into my previous directory alpha883 Programming 4 05-08-2009 09:55 AM
copy specific files with directory stucture rincewind Linux - Software 1 06-22-2006 08:58 AM
how to write a script to create a directory mgam Linux - Newbie 1 10-29-2003 01:08 PM


All times are GMT -5. The time now is 04:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration