September 21, 2007

Rename files in Bash

Some examples how I renamed easily many files:

Say that you have the following files:

pipp.1
pipp.2
...

that need to be changed to:

PPP_1
PPP_2
...

This can be done by the following command:

for i in $(ls pipp*); do a=$(echo $i | cut -d'.' -f2); echo $a; mv $i PPP_$a; done


Second example: you want to remove the "_" from the beginning of the name of the files

_img000336.bmp
...
_img000337.bmp


This can be done by:

for i in `seq 336 599`; do mv _img000$i.bmp img000$i.bmp ;done


Third example : from the files named

01author.mp3
02author.mp3
...

to

author01.mp3
author02.mp3
...

This can be done by:

for i in $(ls); do b=$(echo $i | cut -c 1,2); mv $i author$b.mp3; done

No comments: