Rename the files in all the directories and sub-directories

Hi all,

I have more than 12000 files in 46 different directories and each directory has 2 sub-directories named �dat� or �gridded�. Dat sub-directories have files with extension �jpg.dat� and gridded sub-directories have files with extension �.jpg�.

I need to rename all the files (some parts to be replaced or removed and most of the parts to be rearranged) in all the directories and sundirectories.

Example name of the files:  
 Old names:

 (an example file from gridded subdirectories)
 gridded_yihF-b3861-DNA433-c1-384-20110301-Tp32-CmKan-Sel2-384-MMS-32C-set1-plate1.jpg 

 (an example file from dat subdirectories)
 yihF-b3861-DNA433-c1-384-20110301-Tp32-CmKan-Sel2-384-MMS-32C-set1-plate1.jpg.dat 

 New names:

 tom_dm-MMS_yihF-b3861_1_[1-4]_DNA433-20110301.jpg

 tom_dm-MMS_yihF-b3861_1_[1-4]_DNA433-20110301.jpg.dat

In the new name, words come in this order with these changes:

(1) �gridded� needs to be replaced with �tom� (in all the files from all the gridded subdirectories).
(2) �MMS� needs to come just after �tom� and it can also be �RM� instead of �MMS�
(3) �yihF-b3861� word will be different in every file
(4) �plate1� will be replaced with �1�. It can be �plate2� which needs to be replaced by �2�
(5) �set1� needs to be replaced with �[1-4]�. It can be set2, set3 or set4 and needs to be changed [2-4], [3-4] or [4-4] respectively.
(6) � DNA433-c1-384-20110301� part will different in each file. It needs to be renamed to � DNA433-20110301� but the �c1-384� which needs to be removed, is same in every file.
(7) File extensions �.jpg� or �.jpg.dat� remains the same.

Remaining part of the old name (i. e. -Tp32-CmKan-Sel2-384-) will be left out or deleted.

Thanks in advance. I appreciate your time.

HI guys,

I could write a write a script which does the task but the problem is, it works only on the files which are present in the current directory. I need it to be working on subdirectories as well.
Here is the script:

for file in $(find  -type f -name "*.jpg")
do
  echo $file

     newfile="$(echo $file | sed -e 's/gridded-//g' -e 's/-c1-384-/_/g' -e 's/-Tp32-CmKan-Sel2-384-/_/g' -e 's/-32C-/_/g' | sed 's/\(.*\)-\(.*\)-\(.*\)_\(.*\)_\(.*\)_\(.*\)-\(.*\)\.\(.*\)/\5_\2_\7_\6_\3-\4\.\8/' | sed -e 's/^/mohan_dm-/')"

  echo $newfile
  cp $file $newfile
done

It successfully prints the changed names of the files from subdirectories (echo $newfile) but does not make a copy of files in the subdirectories. That means "cp $file $newfile" is not working.

Please help me out here. Thanks in advance.