PDA

View Full Version : [Solved] rename 124 files with long name to short name


mfaridi
January 25th, 2010, 16:12
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
January 25th, 2010, 16:19
run this script in directory with avi files


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


This should do the trick

mfaridi
January 26th, 2010, 07:18
run this script in directory with avi files


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


This should do the trick

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
January 26th, 2010, 07:28
I think it should work for linux as well, even on bash

mfaridi
January 26th, 2010, 07:30
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
January 26th, 2010, 07:31
think

mfaridi
January 26th, 2010, 07:34
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
January 26th, 2010, 07:38
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
January 26th, 2010, 07:41
Just note, that if file names are chaotic, they will be renamed in order, that might not be the same as series

mfaridi
January 26th, 2010, 07:41
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
January 26th, 2010, 07:45
if you want to learn how to do magic with sh, I suggest this tutorial
http://www.grymoire.com/Unix/Sh.html

graudeejs
January 26th, 2010, 08:04
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
January 26th, 2010, 21:23
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