Copying a directory tree (was: xargs)

Oliver Fromme olli at
Thu Aug 21 12:41:39 UTC 2008

Marcel Grandemange <thavinci at> wrote:
 > I need to copy an entire BSD installation except
 > the /mnt directory to /mnt/pc

You don't need xargs for this.

# cd /
# find -Ed . -regex '\./(mnt|dev)' -prune -or -print0 | cpio -dump0 /mnt/pc

If you have procfs mounted, add "|proc" to the regex,
or simply umount it before.  It's not required for
anything important.

Or -- slightly less efficient, but this might be better
if you're more familiar with grep than with find:

# cd /
# find -d . | egrep --null -v '^\./(mnt|dev)/' | cpio -dump0 /mnt/pc

Yet another way is to use cpdup (from ports/sysutils/cpdup):

# echo mnt > .cpignore
# cpdup -x / /mnt/pc

Note that cpdup doesn't cross mountpoints, so you don't
have to exclude dev or proc.  But you will have to repeat
the command for /var, /usr, /tmp or any other directories
that are on separate file systems.

 > Ls | grep -v proc | xargs cp -Rpv /mnt/pc

Do not use cp -R or cp -r.  It has several problemsm,
for example it breaks hardlinks.  In my opinion the
-R and -r options of cp(1) should be removed, or a big
fat warning message should be printed, because they're
abused most of the time and cause breakage that other
people have to find and clean up afterwards.  Been
there, done that, a thousand times.

Best regards

Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing b. M.
Handelsregister: Registergericht Muenchen, HRA 74606,  Geschäftsfuehrung:
secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht Mün-
chen, HRB 125758,  Geschäftsführer: Maik Bachmann, Olaf Erb, Ralf Gebhart

FreeBSD-Dienstleistungen, -Produkte und mehr:

Perl is worse than Python because people wanted it worse.
        -- Larry Wall

More information about the freebsd-questions mailing list