holographix
holographix

Reputation: 2557

rename a set of files, by changing their prefix

I've got a set of four directories

 English.lproj
 German.lproj
 French.lproj
 Italian.lprj

each of this contains a serie of XMLs named

 2_symbol.xml
 4_symbol.xml
 5_symbol.xml
 ... and so on ...

I need to rename all of these files into another numerical pattern, because the code that determined those numbers have changed. so the new numerical pattern would be like

 1_symbol.xml
 5_symnol.xml
 3_symbol.xml
 ... and so on ...

so there's no algorithm applicable to determine this serie, because of this reason I thought about storing the two numerical series into an array.

I was thinking to a quick way of doing it with a simple bash script. I think that I'd need an array to store the old numerical pattern and another array to store the new numerical pattern, so that I can perform a cycle to make

 # move n_symbol.xml newdir/newval_symbol.xml

any suggestion?

thx n cheers.

-k-

Upvotes: 0

Views: 605

Answers (2)

just somebody
just somebody

Reputation: 19247

you don't need bash for this, any POSIX-compatible shell will do.

repls="1:4 2:1 4:12 5:3"

for pair in $repls; do
  old=${pair%:*}
  new=${pair#*:}
  file=${old}_symbol.xml
  mv $file $new${file#$old}
done

edit: you need to take care of overwriting files. the snippet above clobbers 4_symbol.xml, for example.

for pair in $repls; do
  ...
  mv $file $new${file#$old}.tmp
done
for f in *.tmp; do
  mv $f ${f%.tmp}
done

Upvotes: 2

SiegeX
SiegeX

Reputation: 140317

The following script will randomly shuffle the symbol names of all xml files across 'lproj' directories.

#!/bin/bash

shuffle() { # Taken from http://mywiki.wooledge.org/BashFAQ/026
    local i tmp size max rand

    size=${#array[*]}
    max=$(( 32768 / size * size ))

    for ((i=size-1; i>0; i--)); do
        while (( (rand=$RANDOM) >= max )); do :; done
        rand=$(( rand % (i+1) ))
        tmp=${array[i]} array[i]=${array[rand]} array[rand]=$tmp
    done
}

for file in *lproj/*.xml; do # get an array of symbol names
    tmp=${file##*/}
    array[$((i++))]=${tmp%%_*}
done

shuffle # shuffle the symbol name array

i=0
for file in *lproj/*.xml; do # rename the files with random symbols
    echo mv "$file" "${file%%/*}/${array[$((i++))]}_${file##*_}"
done

Note: Remove the echo in front of the mv when you are satisfied with the results and re-run the script to make the changes permanent.

Script Output

$ ./randomize.sh
mv 1.lproj/1_symbol.xml 1.lproj/16_symbol.xml
mv 1.lproj/2_symbol.xml 1.lproj/12_symbol.xml
mv 1.lproj/3_symbol.xml 1.lproj/6_symbol.xml
mv 1.lproj/4_symbol.xml 1.lproj/4_symbol.xml
mv 2.lproj/5_symbol.xml 2.lproj/14_symbol.xml
mv 2.lproj/6_symbol.xml 2.lproj/1_symbol.xml
mv 2.lproj/7_symbol.xml 2.lproj/3_symbol.xml
mv 2.lproj/8_symbol.xml 2.lproj/7_symbol.xml
mv 3.lproj/10_symbol.xml 3.lproj/10_symbol.xml
mv 3.lproj/11_symbol.xml 3.lproj/11_symbol.xml
mv 3.lproj/12_symbol.xml 3.lproj/2_symbol.xml
mv 3.lproj/9_symbol.xml 3.lproj/8_symbol.xml
mv 4.lproj/13_symbol.xml 4.lproj/13_symbol.xml
mv 4.lproj/14_symbol.xml 4.lproj/15_symbol.xml
mv 4.lproj/15_symbol.xml 4.lproj/9_symbol.xml
mv 4.lproj/16_symbol.xml 4.lproj/5_symbol.xml

Upvotes: 1

Related Questions