cp -r without destination directory?

oniq

Banned
Feb 17, 2002
4,196
0
0
I'm sure its right in front of me, but where do the files go if you do a cp -r /blah/blah/* without a destination directory? :\
 

oniq

Banned
Feb 17, 2002
4,196
0
0
Ok, so it picked a random directory inside that directory and threw everything in there.. lovely.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
cp (and mv) use the last path as the target.

mv onedir twodir threedir

will move onedir and twodir into threedir/

When you glob using *, it expands into multiple paths, each for a file or directory that matches that pattern. Whichever comes last will be seen by cp as the target to copy to.

(the important thing here is that cp does not expand the glob; your shell does that. cp gets whatever results came from that.)

btw, generally, -R is more proper than -r.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Eh.. sitting in front of the computer for long hours?

I didn't even know that -r was a valid option. Gnu cp seems to treat -R and -r the same, but all 3 of the BSDs' man pages do not list -r as an option; instead they say this near the bottom:

Historic versions of the cp utility had a -r option. This implementation
supports that option, however, its use is strongly discouraged, as it
does not correctly copy special files, symbolic links or fifo's.

edit: and here's a simple example of how you can see that the shell expands filenames, not cp:

% echo *.c
blobmodule.c mlist.c mlist2.c mlist3.c mlist4.c mlist5.c

Now if I take out the echo, I can just type the glob, and the shell will then try to execute whatever the first word in the command is:

% *.c
bash: blobmodule.c: command not found