Subject: bug#6104: [Expert] Bug in mv?

From: "Alan Curry" <[email-masked]>
Date: Wed, 5 May 2010 16:10:46 -0500 (GMT+5)

Note: I saw this on bug-coreutils, haven't read the whole thread.

Gene Heskett writes:
> 
> On Tuesday 04 May 2010, Jo=E3o Victor Martins wrote:
> >On Tue, May 04, 2010 at 10:36:19PM -0400, Gene Heskett wrote:
> >> I tried to "mv amanda* /home/amanda/*" as root and which
> >> which I recall I have done successfully several times before.
> >
> >The shell expand * _before_ passing the args to mv.  So mv saw all
> >files starting with 'amanda' and all files (besides . hidden ones) i=
> n
> >/home/amanda/ as arg.  It then picked the last one listed (probably
> >/home/amanda/tmp/) as destination.
> >
> I had two files whose names started with amanda in that directory.  I
>  would
> have assumed it would expand the src pattern of "amanda*" to match on
> ly those

It's not the first * that's the problem. The second one (/home/amanda/*)
expands to a list of everything that was in /home/amanda (except dotfiles)
and that happens before mv is executed. There are several possibilities of
what that command can do:

1. /home/amanda contained no files before the move. In that case the
/home/amanda/* is passed through literally as the final argument to mv, so mv
sees 3 arguments (your 2 files, then "/home/amanda/*" which doesn't exist)
and it fails, because with more than 2 arguments, the last argument must be
an existing directory.

2. /home/amanda contained some stuff, and the last item in the expanded list
(alphabetically sorted) was not a directory. Same result as #1.

3. /home/amanda contained some stuff, and the last item in the expanded list
happened to be a directory (say you have a directory called
/home/amanda/zzzzzzzz): then the list expands, the final argument to mv is an
existing directory, so you have success! Your 2 files, plus everything in
/home/amanda, gets moved into the zzzzzzzz directory. If this isn't what you
meant, you did something wrong. mv just did what it was told.

4. Like #1, but with a nomatch shell option enabled, you get a "No match"
error message.

Your career as a unix wizard isn't complete until you've done something like
#3 *on purpose*.

-- 
Alan Curry