small programming question

Stuart Sears stuart at sjsears.com
Tue May 24 20:40:28 UTC 2005


John Summerfied wrote:
> Bob Chiodini wrote:
> 
>> On Mon, 2005-05-23 at 03:43 -0400, Claude Jones wrote:
>>
>>> Could someone help. I have chosen a bad naming convention for a long 
>>> list, and I need to make a small alteration. The list looks like the 
>>> following
>>> 001-Fig 1-1.jpg   013-Fig 4-4a.jpg  024-Fig 5-6.jpg   035-Fig 5-17.jpg
>>> etc...
>>> I would like to add a 0 to each group of three numbers at the 
>>> beginning of each name so that 001-Fig 1-1.jpg becomes 0010-Fig 
>>> 1--1.jpg for example.
>>> Could someone help me with the quick way to do this? -- 
>>> Claude Jones
>>> Bluemont, VA, USA
>>>
>>
>> Claude,
>>
>> Are you attempting to rename files or edit a list of file names?
>>
>> For the latter, you could use gedit and replace all occurrences "-F"
>> with "0-F".
>>
>> For the former you would need to script it.  Something like:
>>
>> for i in *.jpg; do newname=`echo $i | sed s/-/0-/`; mv "$i" 
>> "$newname"; done
That would work, but is slightly overcomplicated

for i in *.jpg; do newname=`echo $i | sed s/-/0-/`; mv $i 0$i; done

would work just as well (although i am quite fond of a spurious sed or two)

Incidentally, although you already have several really good answers to 
your question, if the files all start with the same character
you could use the oft-ignored 'rename' command

rename 0 00 *.jpg

would replace the *first* 0 in each filename with 00

Stuart





More information about the users mailing list