Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
10-25-2005, 09:19 PM
#1
Member
Registered: Oct 2005
Location: China
Distribution: ArchLinux
Posts: 103
Rep:
about the bash shell command
In a dir, I have lots of file like these :
abcdfasdfdsdfdsfs.mp3,
abdddsfdsfdafsdfdl.mp3,
ds125dsdfsddsd7dsfdsaf.pdd.mp3,
etc.
I want to rename them once.
for examble, I want to make them turned to be
abc_sfs.mp3,
abd_fdl.mp3,
ds1_pdd.mp3,
etc.
If the bash shell can do this ?
Last edited by naihe2010; 10-25-2005 at 09:21 PM .
10-25-2005, 09:46 PM
#2
Member
Registered: Nov 2004
Location: ON, CANADA
Distribution: ubuntu, RHAS, and other unmentionables
Posts: 372
Rep:
yeah, I'm sure there is a shell script for that... I am just not aware of one at the moment.
However, if you are running kde, their is a built-in tool called krename which works great even for many hundreds of files.
10-26-2005, 06:18 PM
#3
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
Hi.
I thought that sounded a bit interesting, so I had a bash myself:
Code:
#!/bin/bash
for FILE in *
do
PREFIX=`echo "$FILE" | cut -b 1-3`
EXTENSION=`echo "$FILE" | sed 's/.*\.//'`
BASE=`basename "$FILE" $EXTENSION`
BASECHARS=`echo "$BASE" | wc | awk '{ print $3 }'`
CUTAT=`expr $BASECHARS - 4`
POSTFIX=`echo $BASE | cut -b $CUTAT-$BASECHARS`
mv "$FILE" "$PREFIX"_"$POSTFIX$EXTENSION"
done
That will rename
every file in the current directory in the way described.
Try it out on a test directory before you use it on anything important.
Dave
10-26-2005, 09:14 PM
#4
Member
Registered: Oct 2005
Location: China
Distribution: ArchLinux
Posts: 103
Original Poster
Rep:
I think because there are lots of Unicode chractors in my file name,they turned to be like :
?????.wma,
????????.mp3,
etc
Thank you all the same.
10-26-2005, 10:14 PM
#5
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
Ah, Unicode.
The only statements in the above code that should be affected by Unicode are the 'cut' statements.
Try this one instead - I've replaced the 'cut's with 'awk'.
Code:
#!/bin/bash
for FILE in *
do
PREFIX=`echo "$FILE" | awk -F "" '{ OFS=""; i=1; print $i,$++i,$++i }'`
EXTENSION=`echo "$FILE" | sed 's/.*\.//'`
BASE=`basename "$FILE" $EXTENSION`
BASECHARS=`echo $BASE | wc -m`
((BASECHARS--))
CUTAT=`expr $BASECHARS - 3`
POSTFIX=`echo $BASE | awk -F "" '{ OFS=""; i='$CUTAT'; print $i,$++i,$++i }'`
mv "$FILE" "$PREFIX"_"$POSTFIX"."$EXTENSION"
done
Dave
10-26-2005, 10:34 PM
#6
Member
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192
Rep:
Yet another way:
Code:
for mp3 in `ls *.mp3 | sed s/.mp3//g`; do
mv $mp3.mp3 ${mp3:0:3}_${mp3:`expr length $mp3 - 3`:3}.mp3
done
Cheers,
10-26-2005, 11:34 PM
#7
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
Quote:
Originally posted by freegianghu
Yet another way:
Code:
for mp3 in `ls *.mp3 | sed s/.mp3//g`; do
mv $mp3.mp3 ${mp3:0:3}_${mp3:`expr length $mp3 - 3`:3}.mp3
done
Cheers,
What happens if you have a file called 'hello.mp3.mp3'? ;-)
Actually, my script above does weird things with very short filenames (e.g. 'a.mp3') and files with no extension...
I like the ${mp3:0:3} notation, though. I've never seen that before - should prove usefull at some point.
Dave
10-27-2005, 12:40 AM
#8
Member
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192
Rep:
Quote:
Originally posted by ilikejam
What happens if you have a file called 'hello.mp3.mp3'? ;-)
Actually, my script above does weird things with very short filenames (e.g. 'a.mp3') and files with no extension...
I like the ${mp3:0:3} notation, though. I've never seen that before - should prove usefull at some point.
Dave
hello.mp3.mp3 =>
hel_mp3.mp3
ds125dsdfsddsd7dsfdsaf.pdd.mp3 =>
ds1_pdd.mp3
Whats about you?
I havent tested with short filename, thanks for your comment!
Cheers up,
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 02:48 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News