Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-05-2012
Registered User
 
Join Date: Jun 2012
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
[Solved] Renaming Multiple Files in Unix

I have mulitiple files in a unix directory e.g.

FILE10001.txt
FILE10002.txt

I want the above two files to get renamed as by a single command. I tried with mv but it does not work

FILE20001.txt
FILE20002.txt

Paresh
Sponsored Links
    #2  
Old 06-05-2012
Registered User
 
Join Date: Mar 2008
Location: Italy - EU
Posts: 172
Thanks: 5
Thanked 35 Times in 35 Posts

Code:
renchar="2"
for file in `ls /directory`
do
prefix=`echo ${file} | cut -c1-4`
suffix=`echo ${file} | cut -c6-`
mv ${file} ${prefix}${renchar}${suffix}
done

Sponsored Links
    #3  
Old 06-05-2012
Registered User
 
Join Date: Jun 2012
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
It gives following error.

Code:
 
ls: 0653-341 The file /directory does not exist


Moderator's Comments:
Please use next time code tags for your code and data

Last edited by vbe; 06-05-2012 at 09:27 AM..
    #4  
Old 06-05-2012
itkamaraj's Avatar
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 Posts
try this


Code:
 
for i in FILE*; do mv $i ${i/FILE1/FILE2}; done

Sponsored Links
    #5  
Old 06-05-2012
Registered User
 
Join Date: Jun 2012
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
On giving the below given command


Code:
for i in FILE*; do mv $i ${i/FILE2/FILE1}; done

It gives error


Code:
ksh: ${i/FILE1/FILE2}: 0403-011 The specified substitution is not valid for this command.

---------- Post updated at 06:26 AM ---------- Previous update was at 06:22 AM ----------

How to restrict below given command so that it operates on files starting with "FILE2" lying in the directory



Code:
renchar="2"
for file in `ls`
do
prefix=`echo ${file} | cut -c1-3`
suffix=`echo ${file} | cut -c5-`
mv ${file} ${prefix}${suffix}
done

multiple files are there in the directory for eg.


Code:
FILE20001.txt
FILE20002.txt
FILE20003.txt


Last edited by Scrutinizer; 06-05-2012 at 07:34 AM.. Reason: code tags
Sponsored Links
    #6  
Old 06-05-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,346
Thanks: 144
Thanked 1,755 Times in 1,592 Posts
Try:

Code:
for i in FILE1*; do 
  mv "$i" "FILE2${i#FILE1}"
done

The Following User Says Thank You to Scrutinizer For This Useful Post:
frappa (06-05-2012)
Sponsored Links
    #7  
Old 06-05-2012
Registered User
 
Join Date: Jun 2012
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Just prefect.....what i was looking for ....Thanks a lot...
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Renaming multiple files Harleyrci Shell Programming and Scripting 9 10-12-2010 06:00 PM
Renaming multiple files siba.s.nayak UNIX for Dummies Questions & Answers 2 07-19-2010 11:00 AM
Renaming multiple files netx UNIX for Dummies Questions & Answers 3 06-15-2010 08:16 AM
renaming multiple files crux123 Shell Programming and Scripting 20 03-01-2010 06:34 PM
Renaming multiple files rmayur UNIX for Dummies Questions & Answers 6 02-26-2004 03:40 AM



All times are GMT -4. The time now is 03:33 AM.