LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-14-2009, 01:03 PM   #1
damianpfister
LQ Newbie
 
Registered: Jul 2009
Posts: 6

Rep: Reputation: 0
Question Rename multiple files in Bash


Hi Everyone

I am trying to find a way of renaming a list of mp3 files in bash, so that letters are added to their names at the beginning.
For example SONG1.MP3 should become a.SONG1.MP3 and SONG2.MP3 b.SONG2.MP3

It is simple enough to change them to a all have a specific characteristic, such as different extension or even numbered rather than "lettered" at the front....but I cannot figure out how to get them into letters.

for x in *.mp3;do let i+=1; mv "$x" ${i}."${x}"; done

...this changes them to be

1.SONG1.MP3
2.SONG2.MP3

...and so on

How do I get

a.SONG1.MP3
b.SONG2.MP3

and so on?

Had thought about having two loops such as

for x in {a..z} and then for x in *.mp3 ....but I fear this will merely run through a..z for each file rather than just a single file before moving to the next letter.

Another thought was using an array...but again I am not sure how to put this into an effective loop.

Any suggestions?
 
Old 11-14-2009, 01:23 PM   #2
lwasserm
Member
 
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
How about using *.mp3 as the argument to your script and letting the shell then take care of assigning all mp3 files to $1, $2, etc. Then you can do something like
Code:
for letter in {a..z}; do
mv "$1" "$letter.$1"
shift
done
You'll have to add your own logic for dealing with more than 26 files.
 
Old 11-14-2009, 02:24 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Using {,{a..z}}{a..z} will give you 702 prefixes...
Kevin Barry

edit: Or 18278 with {,{,{a..z}}{a..z}}{a..z}. Maybe better as this, though:
Code:
echo {,{,{a..z}}{a..z}}{a..z} | tr ' ' '\n' | while read prefix; do #...

Last edited by ta0kira; 11-14-2009 at 02:31 PM.
 
Old 11-14-2009, 04:32 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
If you use KDE, there's a nice app, krename, that you can use for "bulk" renaming of files.
 
Old 11-14-2009, 05:33 PM   #5
damianpfister
LQ Newbie
 
Registered: Jul 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by lwasserm View Post
How about using *.mp3 as the argument to your script and letting the shell then take care of assigning all mp3 files to $1, $2, etc. Then you can do something like
Code:
for letter in {a..z}; do
mv "$1" "$letter.$1"
shift
done
You'll have to add your own logic for dealing with more than 26 files.
Hmm....never thought of making it an argument to my script. So I was over-complicating it by trying to make it all fit inside a single script - ok...lesson learned.

Just put this to the test and it works a treat!
THANKS!
 
Old 11-15-2009, 08:30 AM   #6
lwasserm
Member
 
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
Your idea of using an array would work pretty well also. It could be done like this:

Code:
NAME=(*.mp3)
LETTER=({a..z})
N=0

for ((N=0;$N<${#names[*]};N++)); do
mv "${NAME[$N]}" "${LETTER[$N]}.${NAME[$N]}"
done
taOkira showed how to stack up sequences for more prefix combinations, thanks I didn't know you could do that.

You could also code the for loop to check the prefix letter for 'z' and change it to a longer prefix or however it needs to be done for your application.

Last edited by lwasserm; 11-15-2009 at 08:31 AM.
 
  


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
bash - rename multiple files with dots in filename eldude543 Programming 20 05-20-2016 11:17 PM
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 08:00 AM
Rename multiple files in folders??? adds2one Linux - Software 19 10-05-2006 12:22 AM
How to rename multiple files? Rostfrei Linux - Newbie 3 07-11-2006 06:06 AM

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

All times are GMT -5. The time now is 03:38 PM.

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