2

I want to replicate a complex directory structure from one Linux server to another remote server start at a certain sub-directory.

I don't want the contents of the files, I just want to preserve the structure, owner and permissions as the server to be cloned contains a large volume of data that must not be copied to the new machine.

I'm guessing that this could be done somehow by tar-ing up the folder structure and un-taring it on the remote machine. Or by using rsync to copy without sending the data. Or possibly by using find to create a shell script.

If anyone has the one-liner at hand that accomplishes this, I would appreciate it.

2

You can combine find and cpio to make a list of subdirectories from the current working directory thus:

find . -type d -print | cpio -oO dirs.cpio

To rebuild that directory hierarchy on the remote machine, copy over the dirs.cpio file to the desired location and run:

cpio -iI dirs.cpio
  • Both answers were great but I'm giving the best answer to this for teaching me about cpio. – Stuart Woodward Dec 14 '09 at 1:20
1

one solution might be

find . -type d > dirs

to find the directories, then use that file with the -T option of tar:

tar cpzT dirs -f dest_file.tar.gz

which should create a compressed archive (z) preserving permissions (p) by archiving only the files listed (T dirs). Transfer file, uncompress with

tar xpzf dest_file.tar.gz

and you should be done.

1

Use rsync to recursively copy only directories under a source path to a destination directory:

rsync --archive --verbose --filter="+ */" --filter="- *" /path/to/src/ /path/to/dest/
rsync --archive --verbose --filter="+ */" --filter="- *" /path/to/apache/logs/ user@domain:/path/to/apache/logs/

Using options --include and --exclude instead of --filter rules with an older version of rsync:

rsync --archive --verbose --include="*/" --exclude="- *" /path/to/src/ /path/to/dest/
rsync --archive --verbose --include="*/" --exclude="*" /path/to/apache/logs/ user@domain:/path/to/apache/logs/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.