# rename 124 files with long name to short name



## mfaridi (Jan 25, 2010)

My friend give me Two DVD , with full of pink panther cartoon.
all of this file are AVI . for example 
Pink panther part1 pink with cow.avi
you see each file have long name .
So I want rename all of them to 
pink1.avi
pink2.avi
.
.
.
pink124.avi
I want choose short name for them and save them on my HDD. 
How I can do this ?
If I want rename them one by one , it take long time.


----------



## graudeejs (Jan 25, 2010)

run this script in directory with avi files


```
#!/bin/sh
a=0
for i in `ls *.avi`; do
  mv -f $i `printf "[red]pink[/red]%0.3d.avi" $a`
  a=`expr $a + 1`
done
exit
```

This should do the trick


----------



## mfaridi (Jan 26, 2010)

killasmurf86 said:
			
		

> run this script in directory with avi files
> 
> 
> ```
> ...



Can I this script for Linux BOX or I must modify it .
If I have all parts of Tom and Jerry How I can use this script


----------



## graudeejs (Jan 26, 2010)

I think it should work for linux as well, even on bash


----------



## mfaridi (Jan 26, 2010)

killasmurf86 said:
			
		

> I think it should work for linux as well, even on bash



So what I must do when I have all episode of tom and jerry and all episode of prison break


----------



## graudeejs (Jan 26, 2010)

think


----------



## mfaridi (Jan 26, 2010)

killasmurf86 said:
			
		

> think



I think I must change pink with tom or prison break
but I think it was better , this script rename all files without edit script.
for example I give this original name and script ask me new name and do this for me


----------



## graudeejs (Jan 26, 2010)

well, it's mush easier to change *pink* to *prison_break* {or *prison_break_season_1_* etc} in script, isn't it

look at my first reply, I marked it red


----------



## graudeejs (Jan 26, 2010)

Just note, that if file names are chaotic, they will be renamed in order, that might not be the same as series


----------



## mfaridi (Jan 26, 2010)

killasmurf86 said:
			
		

> well, it's mush easier to change *pink* to *prison_break* {or *prison_break_season_1_* etc} in script, isn't it
> 
> look at my first reply, I marked it red



Yes it is true


----------



## graudeejs (Jan 26, 2010)

if you want to learn how to do magic with sh, I suggest this tutorial
http://www.grymoire.com/Unix/Sh.html


----------



## graudeejs (Jan 26, 2010)

NOTE: script above will fail on files with spaces (and maybe some special characters in file names).
To avoid this you can run this script first
http://forums.freebsd.org/showpost.php?p=4012&postcount=3


----------



## Graaf_van_Vlaanderen (Jan 26, 2010)

killasmurf86 said:
			
		

> NOTE: script above will fail on files with spaces (and maybe some special characters in file names).
> To avoid this you can run this script first
> http://forums.freebsd.org/showpost.php?p=4012&postcount=3



Replace `ls` with something like `find . -name "*"`
"ls" seems to have issues with spaces in file names.
This one removes all empty spaces from files names:


```
#!/bin/sh
find . -name "* *" | while read file
do rename "$file" "`echo "$file" | nawk ' BEGIN {OFS=""} $1=$1 '`"
done
```


----------

