Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
What OS are the files stored on? – Justin Scott Aug 30 '09 at 6:07
Even more importantly, what OS is the renaming to be performed on. – John Gardeniers Aug 30 '09 at 10:49
If they were my files, I'd want leading zeros so they sort more easily: a00b00c00.ext ... a39b15c00.ext – Dennis Williamson Aug 30 '09 at 11:35
Again with the OS question. Any answer we could give is potentially useless until you tell us that. – Le Comte du Merde-fou Aug 30 '09 at 13:23

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:

for f in string02_*.ext; do    # Assuming that you're running this in the directory with the files
    if [ "$a" = "40" ]; then
        b=$(($b + 1))
    if [ "$b" = "40" ]; then
        c=$(($c + 1))
    mv $f a${a}b${b}c${c}.ext
    a=$(($a + 1))
share|improve this answer
"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. – Dennis Williamson Aug 30 '09 at 6:45
By the way, for 640 files, c never increments. – Dennis Williamson Aug 30 '09 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 '09 at 7:06

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,


for f in $(<files.lst)
    mv $f a${k}b${j}c0.ext # rename happens here
    if (( $i % 40 == 0 )); then
            j=$(($j + 1));
            k=$(($k + 1));
    i=$(($i + 1));
share|improve this answer
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 – Dennis Williamson Aug 30 '09 at 11:32

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


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


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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.