It's the shell that expands wildcards, not the commands. So cp dir1/* dir2/*
first expands the two wildcards, then calls cp
on the result. This is not at all what you apparently expect: depending on how many files there are already in dir2
, dir2/*
may expand to one or more argument. The command cp
doesn't know which of its arguments came from expanding the first pattern and which ones came from expanding the second pattern. It expects its last argument to be the name of the destination directory. Thus, to copy all the files from the directory dir1
into the directory dir2
, the last argument must be the directory dir2
:
cp dir1/* dir2
Since *
matches all files, cp
attempts to copy all files. This includes directories: directories are files too. It skips directories, but reports an error. It copies the content of special files such as named pipes (something had better be writing to them, or cp
will block), etc.
To copy only regular files, you need to restrict the matching. In zsh, you can use the glob qualifier .
for that:
cp dir1/*(.) dir2
Other shells don't have this. You can use the find
command to filter on file types. Assuming that you're running non-embedded Linux or Cygwin:
find dir1 -maxdepth 1 -type f -exec cp -t dir2 {} +
On Linux, FreeBSD and OSX:
find dir1 -maxdepth 1 -type f | xargs -I {} cp {} dir2
cp dir1/* dir2
orcp -t dir2 dir1/*
– ctrl-alt-delor Nov 20 '13 at 12:50