1

I'm trying to write a bash command to display my music library sorted by bitrate. The command has to be recursive, as I have files organized like: music/artist/album/song.mp3

This displays all mp3s with bitrate information, but without sorting:

find . -type f -name '*.mp3' -exec file {} \;

If you use the commas in the output of the file command as delimiters, the bitrate is between the fifth and sixth commas, so I tried to sort the command like this:

find . -type f -name '*.mp3' -exec file {} \; | sort -n -t, +5 -6

but it doesn't work. Tips?

EDIT:

The output of that find command is something akin to:

./Stemage/Metroid Metal (Original)/supermetroid_brinstar.mp3: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1, 192 kbps, 44.1 kHz, JntStereo

./Stemage/Metroid Metal (Original)/metroid_kraid.mp3: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1, 192 kbps, 44.1 kHz, JntStereo

./Stemage/Metroid Metal (Original)/metroid_itemcollect.mp3: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1, 192 kbps, 44.1 kHz, JntStereo

./Stemage/Metroid Metal (Original)/metroid_tourianbrain.mp3: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1, 192 kbps, 44.1 kHz, JntStereo

./Stemage/Metroid Metal (Original)/metroid_ending.mp3: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1, 192 kbps, 44.1 kHz, JntStereo

./Stemage/Metroid Metal (Original)/metroid_metaltheme.mp3: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1, 192 kbps, 44.1 kHz, JntStereo

Community
  • 1
  • 1
Goodhank
  • 11
  • 3
  • Does `find . -type f -name '*.mp3' -exec file {} \; | sort -n -t, -k6,6` work? – devnull Jul 23 '13 at 06:10
  • It churned out a file list, but it doesn't appear to be sorted by bitrate. – Goodhank Jul 23 '13 at 06:31
  • Unless you share a few lines of output produced by `find . -type f -name '*.mp3' -exec file {} \;` it's hard to help. – devnull Jul 23 '13 at 06:49
  • Oops, sorry. The output from that find command is something like: `./Bolt Thrower/1988 - In Battle There Is No Law/01 - In Battle There Is No Law.mp3: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1, 320 kbps, 44.1 kHz, Stereo` – Goodhank Jul 23 '13 at 14:45

1 Answers1

2

Try this:

find . -type f -name '*.mp3' -exec file {} \; | sort -t, -nk6

jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • It did output a file list, but they aren't sorted by bitrate. – Goodhank Jul 23 '13 at 06:36
  • @Goodhank Post some sample output from your `find` command so that we can debug. – jaypal singh Jul 23 '13 at 13:14
  • Output of my find command: `./Bolt Thrower/1988 - In Battle There Is No Law/01 - In Battle There Is No Law.mp3: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1, 320 kbps, 44.1 kHz, Stereo ` – Goodhank Jul 23 '13 at 14:46
  • @Goodhank So you want to sort on the `kbps` part or `kHz` part? – jaypal singh Jul 23 '13 at 14:48
  • @Goodhank change the command to `sort -t, -nk5` an you should be all set. – jaypal singh Jul 23 '13 at 14:58
  • The list does appear to be semi-sorted, but there are sections that are out of order. I think this is because the output of file isn't consistent - the kbps info isn't always in the exact same place relative to the commas. – Goodhank Jul 23 '13 at 21:09
  • Could one issue be that some of the song names have spaces, and that is throwing of the sorting algorithm? – SethMMorton Jul 23 '13 at 21:20
  • The '-t,' argument means that the comma is being used as the delimiter, so spaces don't have an effect. – Goodhank Jul 23 '13 at 21:29
  • Is there some way to use a regex to search for a string of the form: xxx kbps (could be 2-4 digits) – Goodhank Jul 23 '13 at 22:03
  • If your intention is reporting then I would recommend using awk to identify fields with Kbps and store them as values and name of the song as key and then use awk sort function. – jaypal singh Jul 23 '13 at 22:23