Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

renaming multiple files 1

Status
Not open for further replies.

mrookley

Technical User
May 21, 2001
7
Hi All

I am trying to create a script where I can rename a list of files but only change the 8th character of the filename.

for example :-

Change 1234567A.TXT to 1234567B.TXT

I have about 100 files to change that are all in 8.3 format.

Any suggestions would be welcomed.

Thanks,
Mark.

 
It's simple in principle, but can you provide a better example? Are you always changing the 8th character from A to B? Are characters 1-7 always numeric?

Greg.
 
The 8th character is always changed and characters 1-7 are both alpha and numeric.
 
if all the files were in the same directory, and no other files were there (that you wanted changed), and they always had the same length (12 charecters), this would work.

for i in `ls` ; do
NEWB=`echo $i | cut -c 1-7`
NEWE=`echo $i | cut -c 9-12`
NEWCHR=X
mv $i "$NEWB$NEWCHR$NEWE"
done
 
I created this script to change the 8th character from 'a' to 'b' using a for loop.

#!/bin/ksh
for file in `ls *.txt`
do
NEWNAME=`echo $file | sed 's/a\./b\./'`
mv $file $NEWNAME
echo $NEWNAME
done


Is this what you wanted??
 
badco - that just happens to work for filenames with numberic filenames except for the 8th character - if there was an 'a' as the first character it would change that as well... crowe's solution looks good to me Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
I have tried crowe's script and works fine. I had to modify the search criteria but the principle was the same.

Thanks for all responses

Mark.
 
I disagree with MikeLacey on the badco scipt. The script only changes character at position before the '.TXT'. The sed changes the character before the '.' from 'A' to 'B'. (such as the 'A' character in 1234567A.TXT). If there was an 'A' in the first character where '1' is it would NOT change that as well.

Just had to clarify that because I used the script and it worked for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top