LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script to create same directory stucture (https://www.linuxquestions.org/questions/linux-newbie-8/script-to-create-same-directory-stucture-4175451868/)

kumarjohn 02-27-2013 03:15 AM

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.

colucix 02-27-2013 04:05 AM

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.

RaviTezu 02-27-2013 04:54 AM

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.

unSpawn 02-27-2013 07:52 AM

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'

kumarjohn 02-28-2013 01:45 AM

Hi,

Thanks for the replies.

rsync worked out any issues.

Quote:

Originally Posted by colucix (Post 4900677)
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.


All times are GMT -5. The time now is 08:46 PM.