Click to See Complete Forum and Search --> : Solaris 8 bug in cp?


stiles
04-30-2001, 03:04 PM
I was playing with the -r flag and I'm confused about why it's acting strange. First here's the -r part of cp's manpage:

-r
Recursive. cp will copy the directory and all its files, including any subdirectories and their files to target

That's all good, but when the target does not exist and I'm copying a dir with subdir's I'm getting strange behavior. From reading the manpage I thought that copy would first mkdir <target> and then within target copy the source dir.

ex. cp dir1 dir2 #where dir2 does not exist but dir1 is a dir with a subdirectory

cp creates dir2 but within dir2 is only the subdirectory :confused: I expected that dir2 would be created and would contain dir1 and dir1's subdirectory, instead cp is acting like mv and is renaming dir1 to dir2 but it keeps the original dir1.

again from cp's manpage:
If target_file does not exist, cp creates a new file named target_file that has the same mode as source_file except that the sticky bit is not set unless the user is superuser; the owner and group of target_file are thoes of the user.

I read that to say that cp should mkdir source_file then perform the recersive copy into that dir, but the cp command acts different if the target does not exist.

[ 30 April 2001: Message edited by: stiles ]

njcajun
05-01-2001, 12:53 PM
Well, I honestly have to say that I've never read the cp manpage thoroughly, but I can tell you after checking with a couple of other admins - all of us just make the directory first. Same in Linux. All the documentation I've seen with this kind of situation (like Redhat's RHCE guide on installing using nfs, where you copy the Redhat dir from cd to disk) tell you to create the directory first. Also - check out cp -R. I sometimes use that out of habit.

Also, don't fall into the trap of thinking that man pages are always without a doubt 100% right and/or up to date. :)

javierm
05-10-2001, 03:52 PM
DUDE!!!

Cp is easy. The source directory has to exist. -r option is used for directories.

Ex:

cp filea fileb
If fileb doesn't exist, it gets created. If it does exist, it gets overwritten.

cp -r /etc /tmp
copy the entire etc directory and its contents to /tmp.

cp -r /etc/* /tmp
copy the contents of etc into tmp.

cp /etc /tmp
fails since etc is a directory. -r needed.

stiles
05-10-2001, 05:50 PM
Originally posted by javierm:
<STRONG>The source directory has to exist. -r option is used for directories.
</STRONG>

First no you the source does not have to exist, I refer to the above mentioned section from cp's man page (it doesn't error out, it just gives what is to me an unexpected result). When you copy a directory where the target directory does not exist it renames the source_dir to the name of target_dir and the source_dir is perserved (it's not blown away like mv). Now if the target_dir exisit the source_dir is copied into the target_dir. Your getting two different actions based on commands that (I thought) according to the man page should act the same.

And yes I get the point to mkdir first to avoid this issue.


cp -r source_dir target_exist

--returns--

target_exist
v
source_dir
v
source_sub_dir


cp -r source_dir target_doesn't_exist

--returns--

target_doesn't_exist
v
source_sub_dir


njcajun what does "pipes are replicated, not read from" mean with the -R flag? I guess that would be named pipes huh?