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/newdirThe 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/origdir2. 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 newdir3. Change destination directory owner, group, & perms to match source:
# chown <origowner> newdir
# chgrp <origgroup> newdir
# chmod <origperms> newdir4. Recursively copy /usr/local/origdir to /save/newdir:a. using "cpio":# cd /usr/local/origdir
# find . -depth -print | cpio -pdmv /save/newdirb. using "copy":# umask 00
# copy -vrom /usr/local/origdir /save/newdirNotes: - 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"