quickie

Christopher Twigg cdtwigg at u.washington.edu
Mon May 22 19:57:19 PDT 2000


The problem is that the shell is going to expand the * before the mv ever
sees it, and mv in 'rename mode' only takes two arguments (source and
destination). Hence, no matter what, if you've got more than one file in
a directory, say

bash$ ls
file1 file2 file3

When you say
bash$ mv *

The shell expands that to:
bash$ mv file1 file2 file3

When mv gets more than 2 arguments, it assumes the first (n-1) arguments
are all the source and only the nth argument is the destination; hence you
have to use some other program to give the filenames to mv one by one if
you want it to act properly, which is exactly what that Perl script does.

Of course, you could always use the Perl "-e" argument, if you're _really_
determined to fit it all on a single command line.


Chris

On Mon, 22 May 2000, R. David Whitlock wrote:


> Yeah, thats actually almost exactly what I did, but I was trying to do it

> in a single command line. I kept trying to use pipes with wildcards, but

> no dice. I bet there's a shell variable for "last match" that I just

> can't think of that would work as thus:

>

> mv * ./$VAR.bak

>

> or something to that effect...

>

> Later,

> David

>

>

>

> On Mon, 22 May 2000, Christopher Twigg wrote:

>

> > Here's how I've always done that:

> >

> > Script:

> > ----

> > #!/usr/bin/perl

> >

> > foreach $arg ( @ARGV )

> > {

> > rename $arg, "$arg.png";

> > }

> > ----

> >

> > Command line:

> > bash$ ./rename.pl *

> >

> > ---

> >

> > Christopher Twigg

> > cdtwigg at u.washington.edu

> >

> > On Mon, 22 May 2000, R. David Whitlock wrote:

> >

> > > ok, here's a quickie:

> > >

> > > how would you rename all files in a directory to the same name plus an

> > > extension on the command line?

> > >

> > > for example, if I have 12 files named "1" "2" "3", etc, how to turn them

> > > all into "1.png" "2.png" "3.png", etc?

> > >

> > > -david

> > >

> > >

> > >

> > >

> >

> >

>

>




More information about the Linux mailing list