ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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!
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.