Author: Steve Borho
Email: steve@meridian.com
Web Page: http://www.meridian.com
Date Submitted: Wed, Apr 29, 1998
Status: Updated Entry
Entry Updated: 5
Releases: | all |
Platform: | all |
Category: | Common Problems with Linux\Unix Commands |
Category Listing: | copying directories |
What is the best way to copy an entire directory tree from one place to another?
The recommended method of copying directory trees from one place to another is
to use cpio. It handles symlinks and device files much better than tar or
cp and is the ONLY way to copy the root directory.
Standard syntax is as follows:
cd /source_dir ; find . -depth -print | cpio -pamd /dest_dir
The find command generates an exhaustive list of filenames and pipes them to
cpio which in turns copies each file to the destination tree.Say, for instance, that you have a RedHat 5.0 system that you would like to
move to a new hard-drive. You would take the following steps:
Assuming your current Linux root directory resides on /dev/hda1 and the new
hard-drive is /dev/hdb.
- Install new hard -drive, partition the and format the partitions.
- Boot from RedHat install floppies in "rescue" mode, get to bash prompt
- mkdir mnt ; mount /dev/hda1 /mnt
- mkdir new_root ; mount /dev/hdb1 /new_root
- if there were ! ! more than one Linux partition on /dev/hda, then mount them
now (ie, if hda2 was /usr, then mount /dev/hda2 /mnt/usr)- cd /mnt ; find . -depth -print | cpio -pamd /new_root
- wait for a while
- cd /new_root/etc
- adjust fstab to point to partitions, fix lilo.conf
- lilo -r /new_root
- umount /new_root ; umount /mnt
- reboot, voila
NOTE: the RedHat rescue floppy adds /mnt/bin and /mnt/sbin and /mnt/usr/bin
to your default PATH, so it is important to mount the old root parition
onto a directory named / mnt, otherwise you have no 'find' or 'cpio' or
'vi' to run.
Of course the cpio man page has a complete description of available options.