*nix Question - How Do I Copy Entire Directory?

Superwormy

Golden Member
Feb 7, 2001
1,637
0
0
I know I can copy files with ' cp file1 file2 /directory/to/copy/to/

But how can I copy an entire directory and all its contents?


Also, how can I delete an entire directory and its contents? It always tell me ' Directory Is Not Empty ' is there a way to override that?


Using FreeBSD btw...
 

Sunner

Elite Member
Oct 9, 1999
11,641
0
76
man rm
man cp

rm -r will remove a directory and everything in it.
cp -r will copy recursively.

man is your friend.
 

DaHitman

Golden Member
Apr 6, 2001
1,158
0
0
If you want to make sure your doing a "duplication" with keeping permissions, ownership, and softlinks intact etc all correctly..

Do this:

cd /<source_directory>
find . -print | cpio -pdvm /<dest_dir>


Example: I want to copy /usr/src to /usr/local/src

cd /usr/src
find . -print | cpio -pdvm /usr/local/src



This is one of the best ways to copy an entire directory tree and ensure that you get an exact duplicate.
 

cleverhandle

Diamond Member
Dec 17, 2001
3,566
3
81
Originally posted by: Electrode
wow, you people sure make it sound complicated

cp -pr source destination

Yeah, no kidding. This version is perfectly fine. If you want to be really safe, use -pR instead of -pr, though in most cases this makes no difference.
 

Fallen Kell

Diamond Member
Oct 9, 1999
6,181
520
126
Or if you have a LOT of data that you are copying across to a different hard drive use this:

tar cf - /<copy dir> | ( cd /<dest dir>; tar xf - )

At least I think I have that command correct :) Basically this compresses the data before sending it across the IDE/SCSI bus, as well as preserves the file permissions, and then uncompresses it at the destination.


hehehe...as for *nix, there is always "more then one way to skin a cat", or in this case copy a directory :)
 

Bremen

Senior member
Mar 22, 2001
658
0
0
um, Fallen Kell? Wouldn't that still have to send the info over the IDE/SCSI bus? Since compression is done by the proc, not the HDD?
 

Sunner

Elite Member
Oct 9, 1999
11,641
0
76
Originally posted by: BingBongWongFooey
haha you're still making it too complicated.

cp -a foo/ bar/

Doesn't work on alot of *NIX's, IIRC correctly FreeBSD is one of the *NIX's it doesn't work on.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
DaHitman or Fallen Kell's way is the most fool-proof because it handles permissions and special files (/dev/ entries, symlinks, pipes, etc) correctly.
 

Electrode

Diamond Member
May 4, 2001
6,063
2
81
Originally posted by: Nothinman
DaHitman or Fallen Kell's way is the most fool-proof because it handles permissions and special files (/dev/ entries, symlinks, pipes, etc) correctly.

as does cp -pR