# How to shuffle slyly a list of file with a bash script

Submitted by Nicola Rainiero on 2017-02-12 (last updated on 2017-02-13)

I present a little bash script to change the alphabetical order in a specified or working folder of files, simply adding a random prefix and checking that the resulting one differs from a fixed number of previous elements. I wrote it to shuffle a list of songs that I usually listen to my poor car radio, but I think that it could be useful also for presentation of a series of pictures via usb pen and a TV. In addition this script has a clean option that can rename the files to the original version, useful for make another mix.

At the beginning, while I was listening to the radio of my car, day by day, I heard the same popular songs, the same news and the same performance of the deejays, also changing the station! Since the music is my partner while I go to the work, I was a little fed up with this daily repeat, so after buying an FM transmittor (10 € roughly) and setting it, I found another problem: this economic gadget has a MP3 player via microSD but does not have the shuffle mode!

Again the same problem: every day the same order of the songs, it was frustrating! So I decided to write a program that is able to change the alphabetical order of my music (I always rename the song simply by artist and title) and I choose the bash terminal because I can use a lot of commands in the script.

## The script

Firstly it removes empty spaces and the special characters (', " and -) from the files and then it shuffles them by adding a random number or letter followed by an underscore. For avoiding compatibility issues I chosen to put only the lower case letters of the English alphabet and the 10 first numbers. Using this command:

cat /dev/urandom | tr -cd 'a-z0-9' | head -c 1

As usual this random feature doesn't prevent that songs of the same artist have a different prefix, so the script adopts a little trick:

• stores the random character in an array;
• checks if the extracted is in it, if true it extracts another one, otherwise it goes on1.

## Requirements

Just a bash shell on a typical linux installation (that includes: cat, sed, mv)

## Usage

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

# SETTINGS:
EXTENSION_IN="mp3" 	# extension of the files to randomize
DEBUG=true    		# if true, print statement after executing the command
iterations=36 		# because the maximum number of iterations (a-z0-9) can only be set to 36

In this case the files to randomize and rename is MP3s, the debug is set to true and the array dimension is 36 (the maximum allowed).

Change the settings if you want, it works in this manner:

./random.sh -shuffle directory --> to add a random prefix to a directory of mp3
./random.sh -clean directory   --> to remove the prefix to a directory of mp3


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

You can find and download my script at the following link:

## Example

If you have a directory called Example that contains these files:

$ls -a Example/ . artist4_elcjbvag.mp3 artist6_mrvtwcsi.mp3 .. artist4_oibiyhoi.mp3 artist6_xdsdgofn.mp3 artist1_qgqdpehr.mp3 artist5_bwycnlex.mp3 artist7_veayccrr.mp3 artist1_sxkldydj.mp3 artist5_ucmgmrvq.mp3 artist8_mgsrktrv.mp3 artist2_glbigbbm.mp3 artist5_udwsekqh.mp3 artist9_kplilabh.mp3 artist3_eedwekko.mp3 artist5_zvyuurby.mp3 artist9_wzwyyzln.mp3 artist3_qucbulng.mp3 artist6_agwbhlfs.mp3 artist3_vwoyruoh.mp3 artist6_hzeqcyin.mp3 You can shuffle this by: ./random.sh -shuffle Example And you obtain: $ ls -a Example/
.                       d_artist1_qgqdpehr.mp3  r_artist6_agwbhlfs.mp3
..                      i_artist5_zvyuurby.mp3  t_artist5_bwycnlex.mp3
1_artist1_sxkldydj.mp3  j_artist8_mgsrktrv.mp3  u_artist6_hzeqcyin.mp3
3_artist9_wzwyyzln.mp3  k_artist5_ucmgmrvq.mp3  x_artist2_glbigbbm.mp3
5_artist4_oibiyhoi.mp3  l_artist3_qucbulng.mp3  y_artist9_kplilabh.mp3
7_artist3_vwoyruoh.mp3  m_artist5_udwsekqh.mp3  z_artist3_eedwekko.mp3
8_artist4_elcjbvag.mp3  n_artist6_mrvtwcsi.mp3
9_artist6_xdsdgofn.mp3  o_artist7_veayccrr.mp3

If debug is set to true, a typical screen could be:

--------------------
The random letter or number is 'm'
--------------------
The processed file is the number: 14
The array contains 13 element/s
The new random element is 'm'
This element is already in the array. The new random one is now '7'
This element is already in the array. The new random one is now 'u'
The array now contains 14 element/s: d 1 x z l 7 8 5 t k m i r u

Original file:
artist6_hzeqcyin.mp3
After:
u_artist6_hzeqcyin.mp3

Rename file:
mv artist6_hzeqcyin.mp3 u_artist6_hzeqcyin.mp3

And so on! If you want to clean the directory, use this command:

./random.sh -clean Example

Et voilà, le jeux sont fait:

\$ ls -a Example/
.                     artist4_elcjbvag.mp3  artist6_mrvtwcsi.mp3
..                    artist4_oibiyhoi.mp3  artist6_xdsdgofn.mp3
artist1_qgqdpehr.mp3  artist5_bwycnlex.mp3  artist7_veayccrr.mp3
artist1_sxkldydj.mp3  artist5_ucmgmrvq.mp3  artist8_mgsrktrv.mp3
artist2_glbigbbm.mp3  artist5_udwsekqh.mp3  artist9_kplilabh.mp3
artist3_eedwekko.mp3  artist5_zvyuurby.mp3  artist9_wzwyyzln.mp3
artist3_qucbulng.mp3  artist6_agwbhlfs.mp3
artist3_vwoyruoh.mp3  artist6_hzeqcyin.mp3

## 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.

### Modified Version

I like your script. It works. I found it from your LinkedIn post about it.

I made a few modifications to it which might make it a bit more bullet proof. See what you think.

http://dl.dropbox.com/u/54584985/fileRandomizer_modified.sh

I have a very old Creative Labs mp3 player that sorts by file names. My newer players seem to sort by ID3 tags - which is a mixed blessing - especially when I get files that were not nicely tagged.

Michael (not verified)

Sun, 02/19/2017 - 10:00

### Nice script!…

Nice script!
If you had a list of all mp3-files in a textfile, you could consider using the 'shuf' command, and then use the output from that to copy files to your external device.