Pidsley, this was a great idea! One of those
why didn't I think of that? moments.
I have an old 500MB Ipod shuffle I use in the car for which this will be perfect.
I had to hack at your code a bit, though, to fit my purposes.
My music is a big rambling directory tree full of stuff from all kinds of sources with all kinds of naming schemes, and yes many of the filenames have spaces. ..
So, I brought in "find" to get files out of the darkest corners, and to cope with those spaces used 'find
stuff -print0 | xargs -0 shuf -e' to get the shuffled list, then the bash builtin "mapfile" (thanks to Dr. Xaos for that tip) to put that in an array. Spaces in filenames are OK, but names containing linebreaks will still mess it up. Added a bit of quoting too.
I don't know if your "du" is different but I need the -c option to get the total space used, combined with a filter in awk: du -c "$DEST" | awk '/total/{print $1}'
Otherwise your script is exactly the same and in a few seconds went through 15GB to give me a directory with 450MB of nicely random music. (with spaces in some filenames)
fwiw:
Code: Select all
#!/bin/bash
# copy music from src to dest
# stop when dest is full enough for mp3 player
SRC="$HOME/music" # where to get the random mix
DEST="$HOME/for_stick" # where to put the random mix
KEEP=("$HOME/music/keep"/*) # songs you always want in the mix
MAX_SIZE=450000 # how big is the mix?
rm $DEST/*.mp3 # delete the old mix
cp "${KEEP[@]}" "$DEST" # music i always want in the mix
mapfile -t alist < <(find -L "$SRC" -type f -name '*.mp3' -print0 | xargs -0 shuf -e)
n=0
for song in "${alist[@]}"; do
let n+=1
cp "$song" "$DEST"
size=$(du -c "$DEST" | awk '/total/{print $1}')
echo $n $size $song
[[ $size -gt $MAX_SIZE ]] && break # stop when it's big enough
done
terminal:
Code: Select all
john@waldorf-bv:~$ bash scripts/stickfiller.sh
1 22320 /home/john/music/Cape Verde - Facebook/Cordas Do Sol - Ehh Boi.mp3
2 80620 /home/john/music/Paris DJs/Paris_DJs_Soundsystem-Celebrating_Joe_Henry.mp3
3 83984 /home/john/music/Anarchy/Thai/Various Thai Rock/ypb02.mp3
4 88204 /home/john/music/CDs/Various (World - 2006) Putumayo Presents Acoustic Africa (va)/07 - Laye Sow Mauritania (Senegal).mp3
5 91604 /home/john/music/singles/Vietnam/HueHuongEnsemble-BaZangBaZeOy.mp3
6 93380 /home/john/music/babeblogue/Baba Zula/06 - San 2 Zortlatmasi.mp3
7 96180 /home/john/music/DCS - Aao Nach Lao/[DP]_Bhangra_Music.mp3
8 100368 /home/john/music/Anarchy/Thai/kikakumono/sure4.mp3
9 118640 /home/john/music/HighLife Haven/One/04 - Ogeli sili obi.mp3
10 121636 /home/john/music/Anarchy/Thai/early_lukthung/p_h_p.mp3
11 128728 /home/john/music/campursari/KecikKecikcampursari.net.mp3
12 136740 /home/john/music/CDs/Bhangra Artists (1991) Bhangra Hit Parade Vol 1 (va)/01 - Track 1.mp3
13 138568 /home/john/music/Anarchy/Thai/early_morlam/chawee.mp3
14 148640 /home/john/music/CDs/Gnawa Diffusion 1999 Bab El Oued Kingston/03 - Kabariou.mp3
15 152956 /home/john/music/Anarchy/Thai/Morlam and Lukthung Greats/jntr14.mp3
16 157768 /home/john/music/babeblogue/Beginner's Guide To Bollywood (3CD)/CD 1 - Vintage Bollywood/14 - Love Story - Kya Gazab Karte Ho Jee.mp3
17 166188 /home/john/music/CDs/Salif Keita - Moffou/03 - Track 3.mp3
18 170388 /home/john/music/Monsoon Country/Cambodia/play_KhmerLer/NekProssJomNab.mp3
19 180452 /home/john/music/The Kinks (2001) BBC Sessions 1964 - 1977/CD2/06 - Money Talks.mp3
20 185504 /home/john/music/Orchestra Baobab - Made In Dakar [2007]/Orchestra Baobab - Aline.mp3
21 193996 /home/john/music/Bekkas Majid - African Gnaoua Blues/02Majid_Bekkas_-_Mrahba.mp3
22 197532 /home/john/music/Anarchy/Thai/Phleng Pua Chiwit/hope01.mp3
23 202268 /home/john/music/lastfm/lastripper/tinar/Geoffrey Oryema/Exile/Makambo.mp3
24 209700 /home/john/music/lastfm/lastripper/tinar/Toumani Diabate's Symmetric Orchestra/Boulevard De L'independance/Mali Sadio.mp3
25 213992 /home/john/music/lastfm/lastripper/tinar/Ali Farka Toure & Toumani Diabate/In the Heart of the Moon/Ai Ga Bani.mp3
26 218896 /home/john/music/CDs/Les Négresses Vertes (1995) Zig Zague/08 - Enfer Et Paradis.mp3
27 222656 /home/john/music/lastfm/lastripper/tinar/Vieux Farka Touré/Vieux Farka Touré/Ma Hine Cocore.mp3
28 229948 /home/john/music/CDs/Orchestra Baobab (2002) Specialist in all styles/04 - Jiin ma jiin ma.mp3
29 238888 /home/john/music/CDs/Salif Keita - Moffou/04 - Track 4.mp3
30 242740 /home/john/music/Anarchy/Thai/Phleng Pua Chiwit/kaimod.mp3
31 245448 /home/john/music/Mambo Kings/07 Melao De Cana (Moo La Lah).mp3
32 251400 /home/john/music/African Head Charge - Songs of Praise/10 My God.mp3
33 253964 /home/john/music/Bally Sagoo - Rising From The East/02._Ek_Ladki.mp3
34 257660 /home/john/music/Monsoon Country/Cambodia/play_KhmerLer/PjerBotChomreang.mp3
35 260740 /home/john/music/CDs/Old Thai-Old Thai pop/old_thai-old_thai_pop-09-track_9.mp3
36 267492 /home/john/music/Khaled & Cheb Mami - 100 Percent Arabica/Khaled & Cheb Mami - 100 Percent Arabica 09 - Laaroubi.mp3
37 274208 /home/john/music/lastfm/lastripper/tinar/Ballake Sissoko/Kora Music From Mali/Kaira.mp3
38 285012 /home/john/music/CDs/Najma - Pukar/Najma-Pukar-06-Track6.mp3
39 303412 /home/john/music/CDs/S. Balachander and others (va)/The_Virtuoso_of_the_Veena_etc.-03-Sufi_musicians-Prelude_to_a_ceremony.mp3
40 307012 /home/john/music/lastfm/lastripper/capev/Elizio/Carpe diem/Hands up (feat. Big Tom).mp3
41 311400 /home/john/music/DCS - Aao Nach Lao/[DP]_Mar_Suteya.mp3
42 323156 /home/john/music/CDs/Moksie Patoe (1998) Greetings From Surinam/05 - Babylon.mp3
43 327048 /home/john/music/Jorge Ben unknown/03 O Amante Vigilante Africano.mp3
44 357248 /home/john/music/singles/Kevin_Ayers_Concert4-5-06.mp3
45 363948 /home/john/music/Khaled & Cheb Mami - 100 Percent Arabica/Khaled & Cheb Mami - 100 Percent Arabica 16 - Les Ombres De La Cite (Heavy Rap).mp3
46 367704 /home/john/music/lastfm/lastripper/tinar/Tarika/Bibiango/Demony.mp3
47 371232 /home/john/music/Feats Don't Fail Me Now/04 Down the Road.mp3
48 377340 /home/john/music/Khaled & Cheb Mami - 100 Percent Arabica/Khaled & Cheb Mami - 100 Percent Arabica 05 - Douha Alia.mp3
49 381080 /home/john/music/CDs/Cape_Verde/12_-_Djurumani_-_Galo_Bedjo.mp3
50 386644 /home/john/music/CDs/Nasida Ria - Keadilan/Nasida_Ria-Keadilan-06-Banyak_Dosa_Bertaubatlah.mp3
51 389900 /home/john/music/Lennon Legend- The Very Best of John Lennon/11 Stand by Me.mp3
52 397628 /home/john/music/Donovan (1966) Sunshine Superman [2005 Remastered]/13 - Superlungs (First Version).mp3
53 401864 /home/john/music/HighLife Haven/One/01 - Builders Brigade Band - apolonia.mp3
54 406996 /home/john/music/Cape Verde - Facebook/Ildo Lobo-MANU.mp3
55 411628 /home/john/music/lastfm/lastripper/tinar/Bonga/O'Melhor De Bonga/Mona Ki Ngi Xica.mp3
56 415784 /home/john/music/Tinariwen_Aman_Aman/pista.mp3
57 431908 /home/john/music/HighLife Haven/One/4 - Ebere egbulam.mp3
58 437372 /home/john/music/CDs/Various (1994) viêt-nam - poésies et chants/04 - yên tuoc tranh ngôn (conversation between the swallow and the sparrow.mp3
59 448844 /home/john/music/CDs/Abdel Ali Slimani (1995) Mraya/09 - Ana Guellile Dub.mp3
60 452220 /home/john/music/Pakistani-Gurus Trilogy - Aghaz/shq.mp3
...I'm definitely going to use this. Thank you!
All code is one.