Technical article 104864

How to copy subdirectory trees using the cpio(C) or copy(C) command

OpenServer 5.0.0

Written · Last revised

Problem

How do I make a copy of a subdirectory tree?

Solution

Here is the procedure to make a copy of a subdirectory tree. The specifics for the example presented are:

Original subdirectory name:    /usr/local/origdir
New subdirectory name:         /save/newdir
The example provided makes a copy of the subdirectory tree
/usr/local/origdir, and puts the copy in a directory named
/save/newdir.  Note that this procedure should be performed
from System Maintenance (Single-User) mode.  Be sure to
manually mount any secondary filesystems you need to access,
and unmount them upon completion.  The steps are:
1. Get current permissions of /usr/local/origdir:
# ls -ld /usr/local/origdir
2. Create the new subdirectory "newdir".  Note that you would
   have to create directories above this if they don't already
   exist.  This example assumes that the /save directory has
   already been created.
# cd /save
# mkdir newdir

3. Change destination directory owner, group, & perms to match source:

# chown <origowner>  newdir
# chgrp <origgroup>  newdir
# chmod <origperms>  newdir
4. Recursively copy /usr/local/origdir to /save/newdir:
a. using "cpio":
# cd /usr/local/origdir
# find . -depth -print | cpio -pdmv /save/newdir
b. using "copy":
# umask 00
# copy -vrom /usr/local/origdir /save/newdir

Notes: - cpio is recommended over the copy command as cpio retains permissions more successfully.

- A `.' instead of an `*' was used in the find command so that
  dot files in the origdir would be included.

- Include the "-l" option to cpio or copy if you wish to link the files rather than making a copy of them.

5. (optional) Verify that the copy was successful:

# dircmp /usr/local/origdir /save/newdir | tee /tmp/dirlog

See also

- Manual pages: cpio(C), mkdir(C) - Other scripts in this database:

TA104861, "How to move a directory tree to another filesystem and reference with symlinks"

From the Xinuos technical article database, record 104864. Reproduced with permission. The original is at www.sco.com/ta/104864, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.