From owner-freebsd-questions Fri Sep 22 23:39:31 2000 Delivered-To: freebsd-questions@freebsd.org Received: from guru.mired.org (zoom0-040.telepath.com [216.14.0.40]) by hub.freebsd.org (Postfix) with SMTP id 02B1437B423 for ; Fri, 22 Sep 2000 23:39:26 -0700 (PDT) Received: (qmail 1463 invoked by uid 100); 23 Sep 2000 06:38:48 -0000 From: Mike Meyer MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <14796.20471.999075.169373@guru.mired.org> Date: Sat, 23 Sep 2000 01:38:47 -0500 (CDT) To: Sriranga Veeraraghavan Cc: questions@freebsd.org Subject: Re: how to copy directory and contents? In-Reply-To: <41622504@toto.iv> X-Mailer: VM 6.72 under 21.1 (patch 10) "Capitol Reef" XEmacs Lucid X-face: "5Mnwy%?j>IIV\)A=):rjWL~NB2aH[}Yq8Z=u~vJ`"(,&SiLvbbz2W`;h9L,Yg`+vb1>RG% *h+%X^n0EZd>TM8_IB;a8F?(Fb"lw'IgCoyM.[Lg#r\ Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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 && find . -depth -print | cpio -pvd $ cp -Rp # Hard links turn into two files $ (cd ; tar cf - .) | (cd ; tar xpf -) $ pax -r -w # must exist, and it creates / 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 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 # if exists as a dir, you get /. 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.