Bash Script Question

Status
Not open for further replies.
I have a list of mp3 files that I'd like to organize consistent with a playlist (a text file listing the tracks in a specific order). I'm familiar with the power of bash scripts to do simple tasks like this, however, I lack the experience to write the script. Any help?

More info: the files are named correctly, but my file explorer organizes files in alphabetical order. What I'm looking for is a script that reads the first line of the text file, finds the matching mp3 file, then prepends the mp3 file name with a sequential number (001, 002, 003, etc) until the last line of the text file is read--leaving my mp3 directory organized the same as the playlist.
 
The track names are descriptive (not music tracks) and match the playlist. But they're not in order. They're in alphabetical order because of the way file explorers default to displaying files. I'd like to organize the tracks according to the playlist so I can listen to them in proper order.

Sadly no track numbers.

I'm on Linux. I wonder if there's a quick way to rename a bunch of files by comparing them to a text file in the terminal--sort of a built-in bash script that does what I'm looking for.

I should add: there are 219 files. Otherwise I'd do it manually.
 

minnmass

Ars Tribunus Militum
2,087
Subscriptor
Okay, I think I misunderstood what you were asking for the first time 'round.

As I now understand it, you have a bunch of existing playlists which have tracks listed in The Right Order. You're looking for a way to rename the files themselves so they'll sort ascii-betically (ie., the way that the file explorer sorts them) in the same order as they are in the playlists. Is that correct?

If so, can you post one sample playlist?

Off the top of my head, though, if each playlist just has a list of files in its own directory, it might be as simple as:
Code:
cat -n $playlist | while read number name ; do mv "$name"  "$( echo `printf "%03d" $number` $name )" ; done

cat -n $playlist will spit out the playlist with line numbers pre-pended. You'll want to replace that with the actual playlist file's name (or a glob that will match all-and-only playlist files; '*m3u' might work).
while read number name will read that in, putting the first (whitespace-delimited) token into "number" and the rest into "name" (so, "number" will hold the track number, and "name" will hold the filename).
printf "%03d" $number will print out "number", with leading zeroes so that the number is 3 digits (or more) wide.
echo `printf "%03d" $number` $name will take that wider number (with the leading zeroes) and tack the filename on the end.
$( echo `printf "%03d" $number` $name ): the $() part is a command substitution, saying "do the stuff inside of the parentheses and put the result here"; the inside builds the destination file name, and the substitution puts that name in mv's destination slot
mv "$name" "$( echo `printf "%03d" $number` $name )" actually moves the file from "name" to "xxx name".

So, that command line will read in the playlist with line numbers; for each line, it'll split apart the line number and file name, then move the file from "name" to "xxx name" (where "xxx" is the file number).

It will only work the way you probably want it to work if the playlist is in the same directory as the files in the playlist.

It will move files around; it would be a very good idea to make a copy of the whole directory first, just in case. I have run that command on a small sample of files, but your 219 files may trigger weird things that my (very simple) sample set of files didn't.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
note that minnmass's suggestion would result in the playlost no longer mapping to the files themselves (as the names have changed).
Assuming the playlost is literally just the file names separated by newlines then it's trivial to recreate it:
Code:
ls > *.mp3 > name_of_new_playlist

Also worth noting this won't play very nice if you want to keep changing the playlist (as you will prepend more and more numbers, it isn't that hard to fix that, but trying to make it a one liner would be a bad idea).

Gotta say I can't see the point in this generally, just use a player that understands the playlist (every single one I've ever seen has some form of support for that) and you're done.

If you're dumping this to a separate file system on some embedded player/old cd player in the car that only plays files as it finds them fair enough.
 

minnmass

Ars Tribunus Militum
2,087
Subscriptor
[url=http:/civis/viewtopic.php?p=26190697#p26190697:18ab9e6s said:
ShuggyCoUk[/url]":18ab9e6s]note that minnmass's suggestion would result in the playlost no longer mapping to the files themselves (as the names have changed).
Assuming the playlost is literally just the file names separated by newlines then it's trivial to recreate it:
Code:
ls > *.mp3 > name_of_new_playlist

... the other option, I suppose, would be to replace mv "$name" "$( echo `printf "%03d" $number` $name )" with cp -l "$name" "$( echo explorer_order/`printf "%03d" $number` $name )" - which will make a hard link-copy in the subdirectory "explorer_order" (which has to exist before running the resulting one-liner). The playlist(s) would still work, and updating the "explorer_order" folder's contents to reflect changes to the playlist would be pretty straightforward: delete the contents of the folder and re-run the original command.

Also, ShuggyCoUk's got an extra redirect in there; it should be:
Code:
ls *.mp3 > name_of_new_playlist
 
Awesome. Thanks for the code and suggestions.

It looks like I'm going to have to tweak a couple things to get it to work for me though. The playlist isn't really a playlist--it's just a text file with the titles of the tracks in a specific order. They're basically the titles of each chapter, but without Ch1 or 1. (or anything like that) before the title.

And the titles in the text file don't match exactly with the titles of the mp3 files because of the ".mp3" extension at the end (and because the mp3s have bitrate information included in their titles).

I'm thinking the easiest way to get this to work would be to just change the text file extension to .m3u, run the modified bash script on the mp3s to get rid of bitrate information in their titles, then add ".mp3" to the end of each line of the text file.

;) or :facepalm: ?
 

minnmass

Ars Tribunus Militum
2,087
Subscriptor
Depending on the file names, you mightn't need to do the first round of renaming: you may be able to get away with using globs, like so
Code:
cat -n $playlist | while read number name ; do mv *"$name"*  "$( echo `printf "%03d" $number` $name )" ; done

Note the *"$name"* - if "$name" is unique across all files, that glob would only get it, but would play nicely with chapter bits in front or bitrate bits at the end. That's a big "if", though: make a backup of the directory first, just in case.

If you want to add ".mp3" to the end of the resulting files, change the bit before the last semicolon to "$( echo `printf "%03d" $number` $name ).mp3" et voilà.

... the playlist's filename needn't end in .m3u. I originally thought that you had a bunch of folders with playlists and mp3s, so using a glob like *m3u would let you crank through one directory at a time by re-running the exact same command without having to change the playlist's filename each time (ie., "cd dir1 <enter> (command) cd ../dir2 <enter> <up><up><enter> cd ../dir3 <enter> <up><up><enter>").
 
It's not liking it. "mv: cannot stat. No such file or directory."

Looks like the bitrate information in the mp3 titles is preventing the script from finding a match to the titles in the text file.

Text file: This is the first chapter
mp3 file: This is the first chapter (bitrate).mp3

The bitrate information is enclosed inside of parenthesis. I don't remember what the command is for matching that glob and deleting it, though.

I'll check it out.
 

minnmass

Ars Tribunus Militum
2,087
Subscriptor
It sounds like the text file might not have quite the same names as the files themselves.
My Computer":ni8veptp said:
$ ls ; echo $name ; touch "this file ${name} (bitrate).mp3" ; ls ; mv *"$name"* "$name" ; ls
This is the first chapter
this file This is the first chapter (bitrate).mp3
This is the first chapter

Is it possible that you have something like
Text file: This is the first chapter
mp3 file: this is the first chapter (bitrate).mp3
? That is, that the capitalization doesn't quite match? Or, are there underscores in one but not the other, or slight spelling differences? If so, this becomes (at the very least) a lot harder to do automagically (especially in a one-liner).

As a debug option, try
Code:
echo "working with \"${name}\" from m3u, using number \"${number}\" and finding file(s):" ; ls *"$name"* ; echo ;
in the body of the loop (ie., "do <that> done" instead of "do <the original body> done").
 
Good catch!

Turns out the chapter names in the text file have characters that can't be used in file names (namely, a plus sign or forward-slash).

That's easy to fix in notepad++ though. I'll give it a run.

Alas,
Code:
cat -n $"list.m3u" | while read number name ; do echo "working with \"${name}\" from m3u, using number \"${number}\" and finding file(s):" ; ls *"$name"* ; echo ; done

Results in "No such file or directory path" for all the files.

Note: I copied five files into a test directory and only added those five files' titles to the m3u.
 

minnmass

Ars Tribunus Militum
2,087
Subscriptor
Plus characters are perfectly legal in many (most?) file systems.

... yeah, I don't know why I thought that might help; iv "mv" can't find a file, neither will "ls"...

You needn't use $"list.m3u" - the $ says that the bit that comes after is a variable. As it happens, that particular construct works (I believe because of the quotes), but you're not using a variable to hold the m3u's filename. cat -n list.m3u will work fine.
 
Status
Not open for further replies.