On Sunday 31 August 2014 15:12, in alt.os.linux, "Lew Pitcher"
<
lew.p...@digitalfreehold.ca> wrote:
> On Sunday 31 August 2014 14:54, in alt.os.linux, "Seamus Okafor"
> <Seamus...@is.invalid> wrote:
>
>> I want to move a series of files to numerical names, e.g.,
>> mv firstfile.jpg file001.jpg
>> mv secondfile.jpg file002.jpg
>> mv thirdfile.jpg file003.jpg
>> etc.
>>
>> I know the syntax for a for loop, e.g.,
>> $ for i in *.jpg do; mv $i $i_00#.jpg; done
>>
>> But, how do I increment the number to be 1, and then 2,
>> and then 3, etc.?
>
> using the $(()) syntax, you can increment a variable.
>
> 15:08 $ COUNT=1
> 15:11 $ echo $COUNT
> 1
>
> 15:11 $ COUNT=$((COUNT + 1))
> 15:11 $ echo $COUNT
> 2
>
> 15:11 $ COUNT=$((COUNT + 1))
> 15:11 $ echo $COUNT
> 3
>
> 15:11 $
So, your loop might look like
FILENO=1
for i in *.jpg ;
do
mv "$i" "file${FILENO}.jpg"
FILENO=$((FILENO + 1))
done
But, that doesn't give you fixed width increments.
We can use printf to fix this
FILENO=1
for i in *.jpg ;
do
mv "$i" "file$(printf '%04d' $((FILENO))).jpg"
FILENO=$((FILENO + 1))
done
or even use bash parameter expansion to pad and truncate the value
FILENO=1
for i in *.jpg ;
do
RFNO=0000${FILENO}
mv "$i" "file${RFNO: -4}.jpg"
FILENO=$((FILENO + 1))
done
HTH