You are not logged in.
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
man rename, for starters.
1000
Offline
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.
//github/
Offline
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
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)
//github/
Offline
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
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
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
great job, work like a charm:) thx;)
Offline