[NTLUG:Discuss] Script question

Steve Baker sjbaker1 at airmail.net
Fri Jan 4 15:37:33 CST 2002


Rick Matthews wrote:

> mv -f /archive/domains.*.2 /archive/domains.*.3
> mv -f /archive/domains.*.1 /archive/domains.*.2
> mv -f /archive/domains.*.0 /archive/domains.*.1
> 
> The asterisk apparently acts as a wildcard during the file selection
> part of the command, but it is acting as a literal in the renaming
> porting of the command (I end up with files named domains.*.3).

You evidently expect whatever was matched by the first '*' to be copied
into the location of the second '*' on each line.  That's not how UNIX/Linux
shells work.  They simply expand each and every string that contains wildcards
into the list of files that match.

What *should* be happening is that the shell (not knowing anything about what
the 'mv' command does) should blindly translate the asterisk into any
existing filename.

So, given files:

  domains.2002-01-04_050500.0
  domains.2002-01-02_052515.1
  domains.2002-01-01_043025.2
  domains.2001-12-30_050500.3

...the first 'mv' command will be expanded to:

  mv -f domains.2002-01-01_043025.2 domains.2001-12-30_050500.3

...and thus rename domains.2002-01-01_043025.2 onto
domains.2001-12-30_050500.3 - and not domains.2002-01-01_043025.3 as you
aparrently expected.

Given that, the second 'mv' command will not now find a match for
/archive/domains.*.2 (because you renamed domains.2002-01-01_043025.2
and there are no other files ending with '.2').  Some shells will flag
an error, others will simply ignore the second wildcard (because it
didn't match any files)...and hence run the command with just one
filename:

  mv -f domains.2002-01-02_052515.1

...which will cause an error to be emitted by the 'mv' program instead.

  mv: missing file argument

However, your shell is evidently set to treat unmatched wildcards
literally.  I think that's a BAD way to set up your shell.  If you
have files created with asterisks in their names, you can get into all kinds
of nasty problems...and nobody *ever* really wants it to do that.

Most shell programs let you choose which of these behaviours you
want.  Do a 'man bash' and look for 'nullglob' for an explanation.
There are lots of options for setting this behaviour in various shells
that Linux supports.

----------------------------- Steve Baker -------------------------------
Mail : <sjbaker1 at airmail.net>   WorkMail: <sjbaker at link.com>
URLs : http://www.sjbaker.org
       http://plib.sf.net http://tuxaqfh.sf.net http://tuxkart.sf.net
       http://prettypoly.sf.net http://freeglut.sf.net
       http://toobular.sf.net   http://lodestone.sf.net




More information about the Discuss mailing list