Rename the files in all the directories and sub-directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename the files in all the directories and sub-directories
# 1  
Old 05-15-2013
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.

Code:
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.

Last edited by Scott; 05-15-2013 at 05:41 PM.. Reason: Code tags, not Icode tags
# 2  
Old 05-21-2013
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename files from multiple directories along with directory indicator

Hi, Friends, i have a requirement where i need to rename my files residing in multiple sub directories and move them to one different directory along with some kind of directory indicator. For eg: test--is my parent directory and it has many files such as a1.txt a2.txt a3.txt ... (5 Replies)
Discussion started by: gnnsprapa
5 Replies

2. Shell Programming and Scripting

Archiving and moving files into directories, creating directories, etc.

how can i move "dataName".sql.gz into a folder called 'database' and then move "$fileName".tar.gz * .htaccess into a folder called 'www' with the entire gzipped file being "$fileName".tar.gz? Is this doable or overly complex. so mydemo--2015-03-23-1500.tar.gz > database -... (5 Replies)
Discussion started by: wyclef
5 Replies

3. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

4. Shell Programming and Scripting

Rename files in sub directories with sequential numbers

I can rename a file with sequential numbers from 1 to N with this script: num=1 for file in *.dat;do mv "$file" "$(printf "%u" $num).txt" let num=num+1 done The script begins with renaming a some.dat file to 1.dat.txt and goes on sequentially renaming other DAT files to... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

5. Shell Programming and Scripting

How to store files/directories in different directories!!

Hi legends, I am writing a script, in that my requirement is, if all the fill types stored in one directory from that we need to separate different different directories based on the file types. for example in a directory(anish). 5 different types files 1- directory 2- .txt files 2- .sh... (1 Reply)
Discussion started by: anishkumarv
1 Replies

6. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

7. Shell Programming and Scripting

Rename files and directories with special characters

Hello guys, I was looking for a shell script that removes all the special characters from the files and the subdirectories recursively. I could not locate it any more. Dose any body have a similar script that dose that? Thanks for the help. AV (0 Replies)
Discussion started by: avatar_007
0 Replies

8. Shell Programming and Scripting

Rename files recursivly in specified directories

Hi, This is what I would like to do. 1. Find all directories named "ByHost" in a specified directory 2. Rename all .plist files inside "ByHost" directories This is the way I have been able to do it so far. #!/bin/sh # # Rename ByHost files # # Thomas Berglund, 13.07.08 # Get the... (2 Replies)
Discussion started by: Thomas Berglund
2 Replies

9. Shell Programming and Scripting

Rename files/directories based on their name

i have hundreds of directories that have to be renamed. the directory structure is fairly uniform which makes the scripting a little simpler. suppose i have many directories like this */*/*/*abc* (in other words i have similar directory names 3 dirs deep that all contain the pattern abc in... (8 Replies)
Discussion started by: quantumechanix
8 Replies
Login or Register to Ask a Question