cp
will always copy the file(s) at the start of the command to the file or directory at the end of the command. The slash doesn't really do much to the arguments, unless the argument is a symlink to a directory. Then having the slash will treat it like a directory while omitting the slash will copy the link itself.
Assuming you want recursively copy any subdirectories in your examples you'd do them like:
cp -r /home/src/somedir /dest
and
cp -r /home/src/somedir/* /dest
The first gets the directory /home/src/somedir
and will copy that argument, the directory itself and all the contents of it, to the last arg, /dest
. It will create the somedir
directory in /dest
if needed and use it if it already exists.
The second addes *
to the end of the first argument, which the shell will expand to be every file that does not start with .
in /home/src/somedir
and will copy all of those files and directories to /dest
without regard to what's already there (except some flags to cp
will make it prompt to overwrite files that will have the same in in the destination as an existing file).
As the comments to this answer have pointed out, there are problems using *
to grab all the files in the directory. One alternative would be to use tar
to do the copy for you
tar -c -C /home/src/somedir . | tar -x -C /dest
this will create a tar file of somedir
without the leading path by using -C
to switch to that directory first. By default tar
will print to stdout
which we will then pipe to another tar
to extract it switching into the desired /dest
directory first. This will also preserve lots of file attributes, which cp
can do as well.
1what specifically do you mean by "ignored"? do you want the copy to continue or to abort?
cp -r /home/src/somedir /dest/
should address both situations, either creating or merging into the existing folder – Frank Thomas – 2015-12-22T17:58:40.760Does it have to be with
cp
? There's probably a correct option torsync
that will do this. – Barmar – 2015-12-25T18:55:06.047