Re: [Help] Renaming files through bash shell scripts

Vinoth Marimuthu <[email protected]>
Newsgroups gmane.org.user-groups.linux.ilugc
Message-ID <CAHoi8=jcJLsoy2srN49sVBAzD=2D9vH8ce0gTK3a7sg76SfBUw@mail.gmail.com>
Hi Baskar,

you can try this

i=1
cd *test*
for name in `ls | sort`;
do
temp=`echo $name | cut -c 1-3`;
if [ $i -lt 10 ]
then
newname=$temp;
newname+="0$i.mp4";
echo $name renamed to $newname;
mv $name $newname
else
newname="$temp$i.mp4";
echo $name renamed to $newname;
mv $name $newname
fi
i=$((i+1));
done
cd ..

change it to directory you want.

I executed in bash and it worked i can see the new files like

M1L01.mp4
M1L02.mp4
M1L03.mp4
M1L04.mp4
M1L05.mp4
M1L06.mp4
M1L07.mp4
M1L08.mp4
M1L09.mp4
M1L10.mp4
M2L11.mp4
M2L12.mp4
M2L13.mp4
M2L14.mp4
M2L15.mp4
M2L16.mp4
M2L17.mp4
M2L18.mp4
M2L19.mp4
M2L20.mp4
M3L21.mp4
M3L22.mp4
M3L23.mp4
M3L24.mp4
M3L25.mp4
M3L26.mp4
M3L27.mp4
M3L28.mp4
M3L29.mp4
M3L30.mp4

I kept the first 3 letter as it is if you want you can remove them in the
newname calculation

Regards,
Vinoth marimuthu


On Wed, Oct 28, 2015 at 8:08 AM, Baskar Selvaraj <baskar-FaTCxNPFk8w/[email protected]>
wrote:

> On Wed, Oct 28, 2015 at 3:03 PM, Shakthi Kannan <[email protected]>
> wrote:
>
> > Hi,
> >
> > --- On Wed, Oct 28, 2015 at 2:55 PM, Baskar Selvaraj
> > <baskar-FaTCxNPFk8w/[email protected]> wrote:
> > | The scripts works, but the results were different, if the value
> > after L is < 10.
> > \--
> >
> > That is the question I had asked in my previous reply. What should be
> > the expected behavior in such a case?
>
>
> There could be 'n' number of modules (referred as M) and those modules may
> have 'n' no.of number of lectures (referred as L).
>
> Now the requirement is to remove the modules from the filenames and have
> only lectures in consecutive numerical order
>
> If the directory has files names like this,
>
> M1L1.mp4
> M1L2.mp4
> M1L3.mp4
> M2L1.mp4
> M2L2.mp4
> M2L3.mp4
> M2L4.mp4
> M3L1.mp4
> M3L2.mp4
>
> Then, the resulting file list should be as below (were M followed by 'n'
> value can be removed)
>
> L1.mp4
> L2.mp4
> L3.mp4
> L4.mp4 (here M2L1 should automatically get renamed to L4)
> L5.mp4
> L6.mp4
> L7.mp4
> L8.mp4 (here M3L1 again should automatically get renamed to L8)
> L9.mp4
>
> S. Baskar
> _______________________________________________
> ILUGC Mailing List:
> http://www.ae.iitm.ac.in/mailman/listinfo/ilugc
> ILUGC Mailing List Guidelines:
> http://ilugc.in/mailinglist-guidelines
>
_______________________________________________
ILUGC Mailing List:
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc
ILUGC Mailing List Guidelines:
http://ilugc.in/mailinglist-guidelines
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.