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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Renaming files with increment 1

Status
Not open for further replies.

uguess

Technical User
Nov 5, 2004
40
CA
Hi Everyone.

I have set of files like this. i038_01.txt i038_02.txt etc. I want to rename them to acx001.txt acx002.txt. Is there way you can rename the files while doing the increment. Can you please help.

Thanks
 
Hi,

do you really need to increment?
Or do you just want to replace the 'i038_' part of the name by 'acx0', keeping last 6 bytes of the name unchanged?
 
Yes i want to increment. I could have rename it as per orignal file.

 
Sorry,
I still don't understand. What are your business needs for incrementing?
It would be much easier to write a command that takes all files beginning with 'i038_', changes this and keeps the rest of the name.
Could you explain please?
 
It's not hard to do this, but it looks like they are already incremented ? If so, then generically you can do this (easier if we can assume you have bash or ksh by the way):

Code:
for i in  i038_*
do
# assumes 99 or less files of course
j=`echo $i | sed s/ i038_/acx0/`
echo "rename $i $j"
# uncomment next line once you are sure this is right
# mv $i $j
done

If you have bash or ksh we can eliminate the sed, though it's really no issue unless you are going to run this over and over again.

This assumes 99 files or less, as per original scheme shown. If that's not true, it has to get a little more complicated (but not all that much).

Tony Lawrence
Linux/Unix/Mac OS X Resources
 
Tony,

yes, it was something along these lines I had in mind.
However the whole question looks like a lazy student's homework to me.
How else would the poster insist on an increment, without giving a reason for this perseverance?
Of course it is possible to solve the task using a real increment like
n=`expr $n + 1`
But this would be far too complicated.

regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top