You are not logged in.

#1 2009-03-05 15:06:30

devil_kc
Member
Registered: 2008-09-14
Posts: 93

[solved]extension change bash script

hi guys. i'm looking for a script that would change file extensions.
a guy from work needs a script that ill search directory for all the files for small, hd and big and then  renames those files from .3gp to .3gpb
so when he starts the script script asks him in which directory he wants to do that and then changes the extensions.
can someone please help me?


thanx in advance:)

Last edited by devil_kc (2009-03-05 20:53:21)

Offline

#2 2009-03-05 15:14:50

byte
Member
From: Düsseldorf (DE)
Registered: 2006-05-01
Posts: 2,046

Re: [solved]extension change bash script

man rename, for starters.


1000

Offline

#3 2009-03-05 15:29:27

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [solved]extension change bash script

writing this off the top of my head so please test in a fake directory before actually using.  it will probably require some tweaking

find ./ -name 'small*' | while read file; do
   new=$(echo $file | sed s/3gp/3gpb/g)
   mv $file $new
done

find ./ -name 'hd*'...

find ./ -name 'big*'...

theres probably some optimizations on combining the finds or something but this should get it done.

rename is really nice and i use it all the time but it's not recursive, which is why i have a few scripts like the above to fix some misnomers in multi-leveled directories.

have fun.

Offline

#4 2009-03-05 15:49:03

devil_kc
Member
Registered: 2008-09-14
Posts: 93

Re: [solved]extension change bash script

brisbin, your script works great but it has one crazy feature. i don't want to use word bug:)
the thing is that the script renames files, but ih there is already a file ... let me show you on an example.

sh script.sh  -- small.3gp goes to small.3gpb
touch small2.3gp
sh script.sh -- small2.3gp goes to small2.3gpb and the small.3gpb goes to small 3gpbb

you see the problem?
i'm quite new to bas scripting. so this is kinda big problem for me

Offline

#5 2009-03-05 15:56:17

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [solved]extension change bash script

i think you can just make find slightly more targeted

find ./ -name 'small*.3gp' should _not_ match small2.3gpb for instance.  try that.

edit: confirmed:

┌─[ 10:56 ][ ~ ]
└─> touch small2.3gp
┌─[ 10:56 ][ ~ ]
└─> touch small2.3gpb
┌─[ 10:57 ][ ~ ]
└─> find ./ -name 'small*.3gp'
./small2.3gp

that's what i like about bash scripting, you can test small parts of your code right from CLI, if you're running bash obviously.

Last edited by brisbin33 (2009-03-05 15:58:53)

Offline

#6 2009-03-05 20:52:54

devil_kc
Member
Registered: 2008-09-14
Posts: 93

Re: [solved]extension change bash script

got it, works great
final product:

find $1 -name '*small*.3gp' | while read file; do
   new=$(echo $file | sed s/3gp/3gpb/g)
   mv $file $new
done

find $1 -name '*big*.3gp' | while read file; do
   new=$(echo $file | sed s/3gp/3gpb/g)
   mv $file $new
done

find $1 -name '*hd*.3gp' | while read file; do
   new=$(echo $file | sed s/3gp/3gpb/g)
   mv $file $new
done

thx guys;)

Offline

#7 2009-03-06 09:56:55

devil_kc
Member
Registered: 2008-09-14
Posts: 93

Re: [solved]extension change bash script

problem:(
it says:
  mv: target `Small.3gpb' is not a directory

the thing is that name of files that need to be renamed are in form "something something Small.3gp" problem seems to be with spaces in file names. can someone help me with this? pls, asap

Offline

#8 2009-03-06 10:17:03

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [solved]extension change bash script

Enclose variables in double quotes:

find "$1" -name '*small*.3gp' | while read file; do
   new=$(echo "$file" | sed s/3gp/3gpb/g)
   mv "$file" "$new"
done

find "$1" -name '*big*.3gp' | while read file; do
   new=$(echo "$file" | sed s/3gp/3gpb/g)
   mv "$file" "$new"
done

find "$1" -name '*hd*.3gp' | while read file; do
   new=$(echo "$file" | sed s/3gp/3gpb/g)
   mv "$file" "$new"
done

Offline

#9 2009-03-06 10:29:35

devil_kc
Member
Registered: 2008-09-14
Posts: 93

Re: [solved]extension change bash script

great job, work like a charm:) thx;)

Offline

Board footer

Powered by FluxBB