Subject: bug#6353: cp and mv with single wild card argument acts as if multiple arguments were entered.

From: Eric Blake <[email-masked]>
Date: Fri, 04 Jun 2010 14:43:33 -0600

On 06/04/2010 10:13 AM, Darwin Gregory wrote:
> I did a quick search of the archive, and didn't find this one, but there
> seems to be a potential logical failure in the cp and mv commands.  It may
> be specific to an implementation, but I feel it is a critical issue.

It is generic to how Unix works.  Basically, globs are expanded by the
shell, prior to the command itself being executed.

> If you execute "cp /path/*" the command expands the wildcard, and treats the
> last file as the destination directory.

As it has always done historically, and was standardized by POSIX.

>  If the last file in /path/ is not a
> directory the command fails, but not with the appropriate error.  However,
> if the last file in the directory (or other wildcard expansion) is a
> directory, it will copy all earlier files in the expansion to that
> directory.

Yep.

> I feel this is an unacceptable outcome for a single argument that is a
> wildcard, since whether it works or not is based on the arbitrary presence
> or absence of a directory as the final element in the wildcard expansion.

But mv has no idea whether you typed a glob or spelled it out on the
command line.

> It would be much better to fail with an error indicating  "missing
> destination file operand" as it does if the first argument does not
> contain a wildcard.

How?  That information is lost by the shell's glob expansion before ls
even starts executing.

> 
> Also, if a wildcard expansion contains exactly 2 elements, the second is
> treated as a target whether or not it is a directory, causing a potential
> overlay of data.

This behavior is required by POSIX, but you can use an alias or shell
function to instead invoke mv -i or cp -i by default, which will catch
common errors like this.

For more information on globbing, see this FAQ:
http://www.gnu.org/software/coreutils/faq/#expr-2-_002a-3-does-not-work

-- 
Eric Blake   [email-masked]    +1-801-349-2682
Libvirt virtualization library http://libvirt.org