# An improved bash script to shuffle files and the MP3 tags too

Submitted by Nicola Rainiero on 2018-04-01 (last updated on 2018-04-02)

The script removes empty spaces and special characters (i.e. ', " and -) from a directory of defined files and then it shuffles them by adding a random number followed by an underscore. For the MP3 files it mixes-up also the ID3 tags and it has a special feature to add them if missing, starting from the filename (only if it is renamed like this: Album_name-Artist_name-Title_name.mp3 or Artist_name-Title_name.mp3).

Thanks to the useful comments followed my previous script: How to shuffle slyly a list of file with a bash script, I have rewritten my first one introducing a new faster shuffle engine and adding a new feature to rename not only the filename but also the id3 tags of the mp3 files. Indeed many modern MP3 players use the tag for sorting and not consider anymore the filename.

First, I want to thanks Sitaram Chamarty, his solution with BASH_REMATCH is pretty simple, elegant and awesome. Now it is faster and it possible to adapt automatically the quantity of zero in the prefix considering the final number of the files (00x if there are more of 100 files and so on).

Second, Joe with his modification to the cleaning process.

I have developed the script some months ago and after four revisions I realised that this is almost the last complete one, so I finally decided to publish it.

## Requirement

• id3v21  only if the extension is mp3

## Usage

First you have to check the SETTINGS section of the script and change if is necessary:

# SETTINGS:
ext=mp3		# extension of the files to shuffle


In this case the files to randomise and rename is MP3

After fixed, it works in this manner:

    ./fileRandomizer.sh -shuffle directory --> to add a random prefix to a directory of mp3
./fileRandomizer.sh -clean directory   --> to remove the prefix to a directory of mp3
./fileRandomizer.sh -tags directory    --> to add id3 tags to a directory of mp3
from the filename: Album-Artist-Title.mp3


NOTE: the directory is not necessary if you have the files in the working directory.

## Example

$ls -a Example/ . fldgkr-scikjzho-tfcmpfvg.mp3 kvjfui-kpmmmmdw-frvvfkvr.mp3 vdnnhn-ydondrlz-nuimzjfu.mp3 .. fytuba-urksaxbg-gpdamiep.mp3 ldxtwg-xsedzjmt-ygccxtym.mp3 vqqjxb-kgtwdpxd-agcykotg.mp3 aqgbfc-xygqxlit-cqcmaxbk.mp3 ibxtvv-moxfcbey-hvcvtnve.mp3 rwfunt-jsjmwrof-fxxyefoy.mp3 xbwhvy-napflydm-oocbrckn.mp3 bmfcgg-unxqktni-hwsrjjoa.mp3 ilulvp-iwippocb-wbzyjked.mp3 rzroiq-azufwutp-vgqahoem.mp3 xfheij-plwlouwx-foqbfusz.mp3 divjyr-uvlfmxdf-sxofuvbf.mp3 iohgdx-koerbnva-osfhsgyq.mp3 sbjrsl-eijzbrbr-fjsrwrhh.mp3 elldzf-oytlqtfq-vneqgafq.mp3 jvbjoe-uarbinej-cyxhfxlt.mp3 txszoi-gjdrpqlm-ytbzmklp.mp3  $ ./fileRandomizer.sh -shuffle Example/
$ls -a Example/ . 05-aqgbfc-xygqxlit-cqcmaxbk.mp3 11-divjyr-uvlfmxdf-sxofuvbf.mp3 17-sbjrsl-eijzbrbr-fjsrwrhh.mp3 .. 06-kvjfui-kpmmmmdw-frvvfkvr.mp3 12-ldxtwg-xsedzjmt-ygccxtym.mp3 18-fytuba-urksaxbg-gpdamiep.mp3 01-rwfunt-jsjmwrof-fxxyefoy.mp3 07-txszoi-gjdrpqlm-ytbzmklp.mp3 13-vdnnhn-ydondrlz-nuimzjfu.mp3 19-elldzf-oytlqtfq-vneqgafq.mp3 02-ibxtvv-moxfcbey-hvcvtnve.mp3 08-jvbjoe-uarbinej-cyxhfxlt.mp3 14-xfheij-plwlouwx-foqbfusz.mp3 20-fldgkr-scikjzho-tfcmpfvg.mp3 03-iohgdx-koerbnva-osfhsgyq.mp3 09-rzroiq-azufwutp-vgqahoem.mp3 15-bmfcgg-unxqktni-hwsrjjoa.mp3 04-xbwhvy-napflydm-oocbrckn.mp3 10-vqqjxb-kgtwdpxd-agcykotg.mp3 16-ilulvp-iwippocb-wbzyjked.mp3  $ ./fileRandomizer.sh -clean Example/
$ls -a Example/ . fldgkr-scikjzho-tfcmpfvg.mp3 kvjfui-kpmmmmdw-frvvfkvr.mp3 vdnnhn-ydondrlz-nuimzjfu.mp3 .. fytuba-urksaxbg-gpdamiep.mp3 ldxtwg-xsedzjmt-ygccxtym.mp3 vqqjxb-kgtwdpxd-agcykotg.mp3 aqgbfc-xygqxlit-cqcmaxbk.mp3 ibxtvv-moxfcbey-hvcvtnve.mp3 rwfunt-jsjmwrof-fxxyefoy.mp3 xbwhvy-napflydm-oocbrckn.mp3 bmfcgg-unxqktni-hwsrjjoa.mp3 ilulvp-iwippocb-wbzyjked.mp3 rzroiq-azufwutp-vgqahoem.mp3 xfheij-plwlouwx-foqbfusz.mp3 divjyr-uvlfmxdf-sxofuvbf.mp3 iohgdx-koerbnva-osfhsgyq.mp3 sbjrsl-eijzbrbr-fjsrwrhh.mp3 elldzf-oytlqtfq-vneqgafq.mp3 jvbjoe-uarbinej-cyxhfxlt.mp3 txszoi-gjdrpqlm-ytbzmklp.mp3  $ ./fileRandomizer.sh -tags Example/

1) aqgbfc_album_2-xygqxlit_artist_1-song_cqcmaxbk.mp3 hasn't the tags, they will be added.

2) aqgbfc_xygqxlit_cqcmaxbk.mp3 hasn't the tags, they will be added.

Do nothing, the "aqgbfc_xygqxlit_cqcmaxbk.mp3" has a wrong format:
only album-artist-title or artist-title is allowed

$id3v2 -l aqgbfc_album_2-xygqxlit_artist_1-song_cqcmaxbk.mp3 id3v1 tag info for aqgbfc_album_2-xygqxlit_artist_1-song_cqcmaxbk.mp3: Title : song cqcmaxbk Artist: xygqxlit artist 1 Album : aqgbfc album 2 Year: 2006, Genre: Unknown (255) Comment: Track: 1 id3v2 tag info for aqgbfc_album_2-xygqxlit_artist_1-song_cqcmaxbk.mp3: TYER (Year): 2006 TRCK (Track number/Position in set): 1 TALB (Album/Movie/Show title): aqgbfc album 2 TPE1 (Lead performer(s)/Soloist(s)): xygqxlit artist 1 TIT2 (Title/songname/content description): song cqcmaxbk$ id3v2 -l aqgbfc_xygqxlit_cqcmaxbk.mp3
id3v1 tag info for aqgbfc_xygqxlit_cqcmaxbk.mp3:
Title  :                                 Artist:
Album  :                                 Year: 2006, Genre: Unknown (255)
Comment:                                 Track: 1
aqgbfc_xygqxlit_cqcmaxbk.mp3: No ID3v2 tag


• 1. I choose this one because it is easy to install and to use, but reading some discussion in the net probably mid3v2 is the most reliable and modern command line tagging tool.

## Related Content:

### Nicola Rainiero

A civil geotechnical engineer with the ambition to facilitate own work with free software for a knowledge and collective sharing. Also, I deal with green energy and in particular shallow geothermal energy. I have always been involved in web design and 3D modelling.