How to do a mass rename? How to do a mass rename? unix unix

How to do a mass rename?


Easiest solution is to use "mmv"

You can write:

mmv "long_name*.txt" "short_#1.txt"

Where the "#1" is replaced by whatever is matched by the first wildcard.Similarly #2 is replaced by the second, etc.

So you do something like

mmv "index*_type*.txt" "t#2_i#1.txt"

To rename index1_type9.txt to t9_i1.txt

mmv is not standard in many Linux distributions but is easily found on the net.


If you are using zsh you can also do this:

autoload zmvzmv 'transform.php?dappName=Test&transformer=YAML&v_id=(*)' '$1.txt'


You write a fairly simple shell script in which the trickiest part is munging the name.

The outline of the script is easy (bash syntax here):

for i in 'transform.php?dappName=Test&transformer=YAML&v_id='*do    mv $i <modified name>done

Modifying the name has many options. I think the easiest is probably an awk one-liner like

`echo $i  |  awk -F'=' '{print $4}'`

so...

for i in 'transform.php?dappName=Test&transformer=YAML&v_id='*do    mv $i `echo $i |  awk -F'=' '{print $4}'`.txt done

update

Okay, as pointed out below, this won't necessarily work for a large enough list of files; the * will overrun the command line length limit. So, then you use:

$ find . -name 'transform.php?dappName=Test&transformer=YAML&v_id=*' -prune -print |while readdo    mv $reply `echo $reply |  awk -F'=' '{print $4}'`.txt done