Renaming the file name for n number of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming the file name for n number of files
# 1  
Old 04-09-2013
Renaming the file name for n number of files

Hi ,

I am kind of new to shell scripting and found a situation to handle ...


I have few files which will be ftpd in to our sustem , the file names needs to be renamed based on condition.




------------
HTML Code:
Eg file names :- 

AE_JUNFOR_2013_MTD_2013-04-09-08-30-09.TXT
AE_JUNFOR_2013_YTD_2013-04-09-08-30-09.TXT
temp_AE_JUNFOR_2013_MTD_2013-04-09-08-30-09.TXT
AE_JUNFOR_2014_MTD_2013-04-09-08-30-09.TXT
AE_JUNFOR_2014_YTD_2013-04-09-08-30-09.TXT
temp_AE_JUNFOR_2014_MTD_2013-04-09-08-30-09.TXT

The files needs to be renamed if the file name contains "JUNFOR" and "2013" in then.

The rename should happen only for JUNFOR and 2013 combinations.

HTML Code:
Resulting File names :- 

AE_FOR13_2013_MTD_2013-04-09-08-30-09.TXT
AE_FOR13_2013_YTD_2013-04-09-08-30-09.TXT
temp_AE_FOR13_2013_MTD_2013-04-09-08-30-09.TXT
AE_JUNFOR_2014_MTD_2013-04-09-08-30-09.TXT
AE_JUNFOR_2014_YTD_2013-04-09-08-30-09.TXT
temp_AE_JUNFOR_2014_MTD_2013-04-09-08-30-09.TXT
The folder can contain any number of files in it .. The first three files for one set and the second and so forth.

Please let me know how to handle this renaming.
# 2  
Old 04-09-2013
Try this, if it echo's the mv command correctly, then remove echo and try
Code:
for file in *JUNFOR_2013*.TXT
do 
  echo "mv $file ${file/JUNFOR/FOR13}"
done

--ahamed
# 3  
Old 04-09-2013
Thank you Ahamed,

I got the following error message when I tried to execute it.


Please find the attachment for the screen shot.

Thank you.
Renaming the file name for n number of files-59161ed6792e1d17jpg
# 4  
Old 04-09-2013
Better
Code:
...
echo mv "$file" "${file/JUNFOR/FOR13}"

and remove the echo to really execute.

Last edited by MadeInGermany; 04-09-2013 at 04:11 PM.. Reason: deleted 2nd proposal
# 5  
Old 04-09-2013
Try this
Code:
for file in *JUNFOR_2013*.TXT
do 
  new_file=${file/JUNFOR/FOR13}
  mv "$file" "$new_file"
done

--ahamed
# 6  
Old 04-09-2013
If your shell does not have built-in substitution, sed can do it
Code:
\ls |
while read filename
do
 newname=`echo "$filename" | sed -n 's/JUNFOR_2013/FOR13_2013/p'`
 [ -n "$newname" ] &&
 echo mv "$filename" "$newname"
done


Last edited by MadeInGermany; 04-09-2013 at 04:08 PM.. Reason: added a filter for exclude other files
# 7  
Old 04-09-2013
Hi


Thank you for your quick response.


For both the solution i am getting the same error


"sh[3]: "${filename/JUNFOR_2013/FOR13_2013}": The specified substitution is not valid for this command."

Is the Mv syntax to conditionally rename is particular for any one version of Unix ?

Here is what I did .

Code:
$touch AE_JUNFOR_2013_MTD_2013-04-09-08-30-09.TXT
$touch AE_JUNFOR_2013_YTD_2013-04-09-08-30-09.TXT
$touch temp_AE_JUNFOR_2013_MTD_2013-04-09-08-30-09.TXT

$for filename in *JUNFOR_2013*.TXT
>do
>  echo mv "$filename" "${filename/JUNFOR_2013/FOR13_2013}"
>done

I got the above error.


Please let me know if I am doing any wrong in executing the commands.

---------- Post updated at 12:36 AM ---------- Previous update was at 12:34 AM ----------

Great .. it worked ... Awesome.

Thanks a lot !! I will implement it in over all script now.

Thanks again.

---------- Post updated at 12:37 AM ---------- Previous update was at 12:36 AM ----------

Thank you MadeInGermany and Ahamed.

Great .. sed - worked ... Awesome.

Thanks a lot !! I will implement it in over all script now.

Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting the XML file and renaming the files

Hello Gurus, I have a requirement to split the xml file into different xml files. Can you please help me with that? Here is my Source XML file <jms-system-resource> <name>PS6SOAJMSModule</name> <target>soa_server1</target> <sub-deployment> ... (3 Replies)
Discussion started by: Siv51427882
3 Replies

2. Shell Programming and Scripting

Copying files to a directory, renaming it if a file with the same name already exists

Hi All, I need to copy files from one directory to another with the files to be renamed while copying if a file with the same name already exists in the target directory. THanks, Dev (2 Replies)
Discussion started by: dev.devil.1983
2 Replies

3. Shell Programming and Scripting

Splitting file into multiple files and renaming them

Hi all, Newbie here. First of all, sorry if I made any mistakes while posting this question in terms of rules. Correct me if I am wrong. :b: I have a .dat file whose name is in the format of 20170311_abc_xyz.dat. The file consists of records whose first column consists of multiple dates in... (2 Replies)
Discussion started by: chanduris
2 Replies

4. Shell Programming and Scripting

Renaming a file with sequence number

Hi team, I need a script for renaming a file with sequence number. script get a file from one directory, /home/billing/Cmm/sms/sms_tmp, append sequence no at the end of file name and move a file to other directory, /home/billing/Cmm/sms/. Actual file is cdr201508271527, and file after... (10 Replies)
Discussion started by: mfaizan40
10 Replies

5. Shell Programming and Scripting

Renaming all files inside a zipped file

Hi, To all the Unix gurus this should be a simple task, but as a newbie I'm finding it hard to crack this. Any help is highly appreciated... Scenario: Step 1 : Move zip file from FTP folder to WORK folder Step 2: Unzip the file "Sample_YYYYMMDDHHMMSS.tar.gz" which contains many file... (10 Replies)
Discussion started by: asandy1234
10 Replies

6. Shell Programming and Scripting

How to count number of files in directory and write to new file with number of files and their name?

Hi! I just want to count number of files in a directory, and write to new text file, with number of files and their name output should look like this,, assume that below one is a new file created by script Number of files in directory = 25 1. a.txt 2. abc.txt 3. asd.dat... (20 Replies)
Discussion started by: Akshay Hegde
20 Replies

7. Shell Programming and Scripting

grepping files and then renaming file

Hi, What is the easiest way to list a directory with 1000s of filenames, grep it for a certain sequence of numbers, and if found to rename the file by the value you are grepping. eg The file I am examining will looks like this: 1234 1224343 2324 244 35665 If I am examining a list... (1 Reply)
Discussion started by: mantis
1 Replies

8. UNIX for Dummies Questions & Answers

Renaming files in one file from names in other

Hi Guys, I have a small problem of renaming multiple files. For example I have names of a set of files in one directory like K2_34625-34675 K7_988963-988983 K12_773882-7734102 and the other set corresponding to the same is U_P_321_9_3_11.ab1 U_P_322_9_3_11.ab1 U_P_323_9_3_11.ab1 Now... (23 Replies)
Discussion started by: pawannoel
23 Replies

9. Shell Programming and Scripting

Renaming files by matching info from a separate file

Hi All, I could use a bit of help with this as I'm at a loss. I have a number of files all named accordingly: I have a separate text file that is as follows: What I want to do is end up with: I don't know how to do this, though I'm certain it can be done, and would rather learn how... (14 Replies)
Discussion started by: Demosthenes
14 Replies

10. UNIX for Dummies Questions & Answers

Renaming a Number of Files

I want to rename my mp3s. Iw ant them to use a standard of 'Band Name - Song Title.mp3'. Currently, all of my mp3s are in the following directory structure: /MyMusic/Band Name/Album Name/*.mp3 Esentailly I want to update each file with the Band Name from the hierarchy and then move all... (2 Replies)
Discussion started by: fnard
2 Replies
Login or Register to Ask a Question