[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: batch renaming



Tzafrir Cohen <tzafrir@technion.ac.il> writes:

> and yet another way of doing the equvalent of dos's "move *.for *.f",

As usual, there are zillions different ways to do it. It is my
impression, however, that most solutions offered are limited in
one way or another. A rather general solution would be compiling
a list of files to be renamed, and then in a loop manipulate the names
with a regular expression editor such as sed, e.g. with

mymv ()  
{  
    old=$1; 
    new=$2; 
    shift 2; 
    for f in $@; 
    do 
        mv -fv $f $(echo $f | sed 's/'$old'/'$new'/g'); 
    done 
} 

you will be able to do things like

$ for foo in 1 2 3 4 5 6 7 8 9; do touch foo${foo}foo; done 
$ ls foo[1-9]foo 
foo1foo  foo3foo  foo5foo  foo7foo  foo9foo 
foo2foo  foo4foo  foo6foo  foo8foo 

and now you have a choice of

$ mymv "^foo" bar $(ls foo[1-9]foo)
foo1foo -> bar1foo 
foo2foo -> bar2foo 
foo3foo -> bar3foo 
foo4foo -> bar4foo 
foo5foo -> bar5foo 
foo6foo -> bar6foo 
foo7foo -> bar7foo 
foo8foo -> bar8foo 
foo9foo -> bar9foo 

$ mymv "foo$" bar $(ls foo[1-9]foo) 
foo1foo -> foo1bar 
foo2foo -> foo2bar 
foo3foo -> foo3bar 
foo4foo -> foo4bar 
foo5foo -> foo5bar 
foo6foo -> foo6bar 
foo7foo -> foo7bar 
foo8foo -> foo8bar 
foo9foo -> foo9bar 

$ mymv foo bar $(ls foo[1-9]foo) 
foo1foo -> bar1bar 
foo2foo -> bar2bar 
foo3foo -> bar3bar 
foo4foo -> bar4bar 
foo5foo -> bar5bar 
foo6foo -> bar6bar 
foo7foo -> bar7bar 
foo8foo -> bar8bar 
foo9foo -> bar9bar 

I am not claiming that this is the most general solution possible, and
I didn't find ren (suggested by frodo, I believe) on the Linux system
I have access to at the moment, so I don't know how it works. The
above solution can be improved in many ways (error handling, for one,
I didn't check how back substitution works, etc), but I just wanted to
give a hint. Chen will probably say that perl has a great regexp
engine, but what if perl is not installed?

-- 
Oleg Goldshmidt | BLOOMBERG L.P. (BFM) | oleg@bfr.co.il
"... We work by wit, and not by witchcraft;
 And wit depends on dilatory time." - W. Shakespeare.

=================================================================
To unsubscribe, send mail to linux-il-request@linux.org.il with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail linux-il-request@linux.org.il