LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 10-25-2005, 09:19 PM   #1
naihe2010
Member
 
Registered: Oct 2005
Location: China
Distribution: ArchLinux
Posts: 103

Rep: Reputation: 15
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.
 
Old 10-25-2005, 09:46 PM   #2
nilleso
Member
 
Registered: Nov 2004
Location: ON, CANADA
Distribution: ubuntu, RHAS, and other unmentionables
Posts: 372

Rep: Reputation: 31
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.
 
Old 10-26-2005, 06:18 PM   #3
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
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
 
Old 10-26-2005, 09:14 PM   #4
naihe2010
Member
 
Registered: Oct 2005
Location: China
Distribution: ArchLinux
Posts: 103

Original Poster
Rep: Reputation: 15
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.
 
Old 10-26-2005, 10:14 PM   #5
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
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
 
Old 10-26-2005, 10:34 PM   #6
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
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,
 
Old 10-26-2005, 11:34 PM   #7
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
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
 
Old 10-27-2005, 12:40 AM   #8
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
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,
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
goto/label command for scripting in bash shell terry.trent Linux - Software 3 07-09-2010 10:15 AM
how-to: repeat OR iterate shell OR bash command delay OR interval admarshall Linux - General 5 07-18-2005 10:47 PM
bash shell command line expansion hansi umayangan Linux - General 2 03-13-2005 11:31 AM
Bash shell command on windows98 emailssent General 3 10-16-2004 06:25 AM
Specifying target directory for command in bash shell script? spectrescape Programming 1 07-22-2004 05:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration