Operating System - Linux
1753441 Members
4540 Online
108794 Solutions
New Discussion юеВ

Re: Recreate exact directory structure in another server

 
SOLVED
Go to solution
kenny chia
Regular Advisor

Recreate exact directory structure in another server

Hi
I like to recreate a directory hierachy structure from one server to another. Is there a script to do this?
Requirements
1. Recreated dir must have same permissions, user name and group name
2. Symbolic links to directory must also be recreated as symbolic links (no deferencing)

I was thinking of using tar together with find. But find command cannot differentiate symbolically linked directories and symbolic linked files

Thanks
All Your Bases Are Belong To Us!
8 REPLIES 8
Stuart Browne
Honored Contributor

Re: Recreate exact directory structure in another server

If you just want to copy the directory structure, and not the files, you can use find with tar or cpio quite easily.

find . ! -type f | cpio -ovH newc > /tmp/dir.structure.cpio
One long-haired git at your service...
kenny chia
Regular Advisor

Re: Recreate exact directory structure in another server

Hi
Thanks, but using

find . ! -type f

will also back up symbolically linked files which I have to exclude

Thanks
All Your Bases Are Belong To Us!
Stuart Browne
Honored Contributor
Solution

Re: Recreate exact directory structure in another server

So you want directories, and symbolic links to directories, but not symbolic links to files?

Ugh, that makes life a bit more difficult because of 2).

Ok, try:

find . ! -type f ! -xtype f | ...
One long-haired git at your service...
James R. Ferguson
Acclaimed Contributor

Re: Recreate exact directory structure in another server

Hi Kenny:

Linux/GNU has features that one would like to see in Unix!

An approach other than Stuart's very elegant one is to use Perl:

# cat ./showdir
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use Cwd qw( realpath );
my @dir = @ARGV ? @ARGV : ('.');
find(
sub {
lstat;
my $name = $File::Find::name;
if ( -d _ ) { print "d $name\n" }
elsif ( -l _ && -d realpath($name) ) {
print "l $name -> ", realpath($name), "\n";
}
},
@dir
);
1; #_jrf_

...run as:

# ./showdir /path

For example:

# .showdir /home/jrf
d /home/jrf
l /home/jrf/linktotmp -> /tmp
d /home/jrf/Mail
...

Regards!

...JRF...

Court Campbell
Honored Contributor

Re: Recreate exact directory structure in another server

James,

You may already know about this. But your in luck.

http://hpux.cs.utah.edu/hppd/hpux/Gnu/findutils-4.2.31/
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
cdemmert
Occasional Contributor

Re: Recreate exact directory structure in another server

When I want to copy a directory from one server to another I use the command

tar cf - | ssh 'cd ;tar xpvf -'

I hope you find it useful!

James R. Ferguson
Acclaimed Contributor

Re: Recreate exact directory structure in another server

HI (again) :

Court, thanks, and yes I was aware of the HP-UX port. I'm a tool-builder for a variety of reasons, not the least of which --- TMTOWTDI.

Regards!

...JRF...

kenny chia
Regular Advisor

Re: Recreate exact directory structure in another server

Hi Stuart
Thanks for the tip..
I will use
# find . ! -type f ! -xtype f

But now I have a strange problem regarding file modification time

I used your suggested method to create the cpio file.

# find /home ! -type f ! -xtype f | cpio -ovH newc > file.cpio

I transfer file.cpio to another server and do an extraction using

# cpio -ivmH newc < file.cpio

Did an "ls -l" command and found that all the directories created have a modification time of "now". Very strange..

Run the same command again

# cpio -ivmH newc < file.cpio

Did an "ls -l" command and this time the file modification time is set to the correct original value!

Is there a bug in cpio or did I use the command wrongly?

Thanks
All Your Bases Are Belong To Us!