Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 23 Sep 2000 01:38:47 -0500 (CDT)
From:      Mike Meyer <mwm@mired.org>
To:        Sriranga Veeraraghavan <ranga@CSUA.Berkeley.EDU>
Cc:        questions@freebsd.org
Subject:   Re: how to copy directory and contents? 
Message-ID:  <14796.20471.999075.169373@guru.mired.org>
In-Reply-To: <41622504@toto.iv>

next in thread | previous in thread | raw e-mail | index | archive | help
This being Unix, the problem is to figure out which solution is best
for your problem, not finding one at all.

Sriranga Veeraraghavan writes:
> > How do you copy a directory? I have used cp to copy files, but I am
> > not sure about copying a directory and all the files within. 
> I find that cpio is the prefered way to do this (several others have
> mentioned tar):
> $ cd <src> && find . -depth -print | cpio -pvd <dest>

$ cp -Rp <src> <dest>	    # Hard links turn into two files

$ (cd <src>; tar cf - .) | (cd <dest>; tar xpf -)

$ pax -r -w <src> <dest>    # <dest> must exist, and it creates <dest>/<src>

There's also cpdup in the ports tree, which I'm not familiar with.

Cpio is also a bit safer using null-terminated strings instead of
newline terminated strings (you can put newlines in file names on
Unix):

$ cd src && find . -depth -print0 | cpio -pd0 <dest>

Of course, if the two directories are on the same file system, mv is
the most efficient way - it just creates the new name, then unlinks
the old one.

$ mv <src> <dest>	    # if <dest> exists as a dir, you get <dest>/<src>.

cp, cpio, pax and tar have flags to preserve various things which you
might or might not want to preserve. They can also be set to be
verbose about what they're doing. pax & cpio can be set to use hard
links if possible, which might or might not be useful. Likewise, all
but mv can be made to actually copy symbolic links as the file they
point to instead of the symlink.

	<mike


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?14796.20471.999075.169373>