Re: Batch file rename in Linux (consecutive numbers)
On Sep 2, 1:33 am, spce...@armory.com (John DuBois) wrote:
> In article <1188589094.698861.37...@i38g2000prf.googlegroups. com>,
>
>
>
> gibbonsp <gibbo...@gmail.com> wrote:
> >I need to rename a batch of files:
>
> >foo_001.jpg
> >foo_002.jpg
> >foo_003.jpg
> >bar_001.jpg
> >bar_002.jpg
> >bar_003.jpg
> >....and so on.
>
> >I need to take the number on these files (notice how the number is
> >duplicated on foo and bar) and make the entire batch consecutive. In
> >other words, I want the file list to look like this:
>
> >foo_001.jpg
> >foo_002.jpg
> >foo_003.jpg
> >bar_004.jpg
> >bar_005.jpg
> >bar_006.jpg
> >....and so on.
>
> >I'd appreciate any ideas! I just feel like this is so simple and I'm
> >just not seeing the answer!
Assuming that your filenames follow the pattern .*_¥d{3}.jpg
#!/bin/bash
#dir files contains all the files
files=`ls files`
i=1
padding="000"
for file in $files
do
((len=3-${#i}))
j="${padding:0:${len}}${i}"
newName=${file/_???./_${j}.}
mv files/${file} files/${newName}
((i=i+1))
done
|