0

I have 640 files in the following form

string02_01.ext, string02_02.ext, string02_03.ext  ...

I need to rename all of them so that every 40 items it changes pattern, for instance

first  40s: a0b0c0.ext, a1b0c0.ext, a2b0c0.ext ...
second 40s: a0b1c0.ext, a1b1c0.ext, a2b1c0.ext ...
third  40s: a0b2c0.ext, a1b2c0.ext, a2b2c0.ext ...
fourth 40s: a0b3c0.ext, a1b3c0.ext, a2b3c0.ext ...

What would be a good way of doing this? Any ideas will be appreciate it.

Thanks in advance

4
  • 1
    What OS are the files stored on? Aug 30, 2009 at 6:07
  • Even more importantly, what OS is the renaming to be performed on. Aug 30, 2009 at 10:49
  • 1
    If they were my files, I'd want leading zeros so they sort more easily: a00b00c00.ext ... a39b15c00.ext Aug 30, 2009 at 11:35
  • Again with the OS question. Any answer we could give is potentially useless until you tell us that. Aug 30, 2009 at 13:23

3 Answers 3

1

You don't want a regex for this, you want a regular shell script that iterates over the files as required. Assuming that you want that 40th file to be named a39b0c0.ext, something like this shell script should do the trick:

a=0
b=0
c=0
for f in string02_*.ext; do    # Assuming that you're running this in the directory with the files
    if [ "$a" = "40" ]; then
        a=0
        b=$(($b + 1))
    fi
    if [ "$b" = "40" ]; then
        b=0
        c=$(($c + 1))
    fi
    mv $f a${a}b${b}c${c}.ext
    a=$(($a + 1))
done
3
  • "Assuming that you want that 40th file to be named a40b0c0.ext" - using your code, it would be a0b1c0.ext, which seems to be what he wants. Aug 30, 2009 at 6:45
  • By the way, for 640 files, c never increments. Aug 30, 2009 at 6:48
  • "for 640 files..." sure, but I like to be extensible. You're (half) right as to the naming at the 40th file; it won't be a0b1c0, it'll be a39b0c0 (since a=40 will be hit on the 41st file); I've corrected my answer to suit.
    – womble
    Aug 30, 2009 at 7:06
1

This is how I expect your 640 file names to be organized,

Row  1: a0b0c0.ext a1b0c0.ext ... a39b0c0.ext
Row  2: a0b1c0.ext a1b1c0.ext ... a39b1c0.ext
...
Row 16: a0b15c0.ext a1b15c0.ext ... a39b15c0.ext

Assuming that you have a list of 640 files (one per line),
sorted in the order you want them to be renamed.
(that can be done using the following commands).

cd /directory/containint/the/files
find . -name string02_*.ext > files.lst
# maybe you'll run a 'sort' pipe to get files.lst ordered as you want.

Note that your 640 files cannot be of the form string02_nn.ext,
They are probably `string02_nnn.ext then....
does not matter since we use the above scheme.


The bash script for renaming should look like this,

#!/bin/bash
i=0;
j=0;
k=0;

for f in $(<files.lst)
do
    mv $f a${k}b${j}c0.ext # rename happens here
    if (( $i % 40 == 0 )); then
            j=$(($j + 1));
            k=0;
    else
            k=$(($k + 1));
    fi
    i=$(($i + 1));
done
1
  • It's not necessary to make a file containing the names, just use shell globbing (as in womble's answer): for f in string02_*.ext Aug 30, 2009 at 11:32
0

not sure it's an exact match for your needs, but take a look at mmv. it can do mass renames like:

file1.txt
file2.txt
file3.txt

mmv "file*.txt" "#1_file.txt"

1_file.txt
2_file.txt
3_file.txt

if you use a Debian like distro, just apt-get mmv.

You must log in to answer this question.