Register
It is currently Mon Jul 28, 2014 10:35 pm

mp3 player random auto-filler


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Mon Jun 04, 2007 2:00 pm   

Joined: Mon Jun 04, 2007 1:43 pm
Posts: 3
hi to all
first i'd like to thank you for yours support in the chat #channel always great and really kind: in the past yours support help me for automate some simply but usefull process

second: the problem
i have a semi-newbie knowledge, but i'd like to write a script for: take some files (mp3 in this case) randomly from a folder (and its subfolders) and copy they in another one (i dunno if better to mirroring the directory structure or not.. doesn't matter) until the free space reach a desired value (% or size, approx obv: no prob for 10/20 megs difference, and specified in the script ): can u give me some suggestion for what kind of commands i can use for make this script?


let me know, regards :idea:
and sorry for the language :(


Top
 Profile  
 PostPosted: Mon Jun 04, 2007 2:25 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hi ^Albe^, welcome to the forums!

Thanks for posting your question here :) Normally I can hack something together, but work is busy today... I've got some ideas that I'll post later


Top
 Profile YIM  
 PostPosted: Mon Jun 04, 2007 5:14 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Okay, there are many ways for this script to go wrong (it may not handle spaces in filenames very well, etc...) but it should give you a good idea of some of the options that you have. I didn't have any mp3's to play with, so I made some random txt files to test.

Code:
#!/bin/bash

## let's set some variables:
## This is our SOURCE directory

DIR="mp3"


## This is our DESTINATION direction

DIRTOO="mp3too"


## A temporary file to keep our numbered list

TMPFILE="/tmp/randommp3"


## The max size of the resulting directory
## (give or take the size of one mp3)

THRESHOLD="100000"


## The amount of space CURRENTLY used
## in the destination directory

SPACE=$(du -s "$DIRTOO"|awk '{print $1}')


## The number of files that we're working with

FILENUM=$(ls "$DIR"| grep -c 'mp3')


## This generates our numbered list and stores
## it in the tmpfile

find "$DIR" -name '*.mp3'|cat -n > "$TMPFILE"


## this starts a loop which ends when our threshold
## ($THRESHOLD) is reached

until [ "$SPACE" -gt "$THRESHOLD" ]; do


  ## This generates our random number based on
  ## the number of files in the source dir.

  RN="$((RANDOM%${FILENUM}+1))"


  ## This cuts out the file name that corresponds to the
  ## random number

  MP3="$(grep -P "^\s*$RN\t" "$TMPFILE" | awk '{print $2}')"


  ## Here we do the actual copy

  cp -v "$MP3" "$DIRTOO"


  ## Here we check to see how much space we are using
  ## before we start the loop again

  SPACE="$(du -s "$DIRTOO"|awk '{print $1}')"


## End of the loop!

done



I hope this helps! Let us know if you have any questions. If you run into specific problems, we can figure out how to work around them.


Top
 Profile YIM  
 PostPosted: Mon Jun 04, 2007 5:16 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
I wonder if it would be easier to generate a playlist instead of copying the actual mp3s?...


Top
 Profile YIM  
 PostPosted: Tue Jun 05, 2007 1:36 pm   

Joined: Mon Jun 04, 2007 1:43 pm
Posts: 3
really thx
the playlist is not a good choice beacuse the destination folder is a sd card (/media/Scheda_sd) used in another device
for the space problem i can rename all the files, no prob at all

using an sd memory, perhaps is better to control the freespace instead the folder dimension
and i have a little error during the execution : grep -P option not available (i think that is a good translation of the error)
using this:
Code:
#!/bin/bash

## let's set some variables:
## This is our SOURCE directory

DIR="/media/Hd_Backup/Mp3"


## This is our DESTINATION direction

DIRTOO="/media/Scheda_sd/mp3"


## A temporary file to keep our numbered list

TMPFILE="/tmp/randommp3"


## The max size of the resulting directory
## (give or take the size of one mp3)

THRESHOLD="100000"


## The amount of space CURRENTLY used
## in the destination directory

SPACE=$(du -s "$DIRTOO"|awk '{print $1}')


## The number of files that we're working with

FILENUM=$(ls "$DIR"| grep -c 'mp3')


## This generates our numbered list and stores
## it in the tmpfile

find "$DIR" -name '*.mp3'|cat -n > "$TMPFILE"


## this starts a loop which ends when our threshold
## ($THRESHOLD) is reached

until [ "$SPACE" -gt "$THRESHOLD" ]; do


  ## This generates our random number based on
  ## the number of files in the source dir.

  RN="$((RANDOM%${FILENUM}+1))"


  ## This cuts out the file name that corresponds to the
  ## random number

  MP3="$(grep -P "^\s*$RN\t" "$TMPFILE" | awk '{print $2}')"


  ## Here we do the actual copy

  cp -v "$MP3" "$DIRTOO"


  ## Here we check to see how much space we are using
  ## before we start the loop again

  SPACE="$(du -s "$DIRTOO"|awk '{print $1}')"


## End of the loop!

done


in the meantime, i study the script for try to understand it


Top
 Profile  
 PostPosted: Thu Jun 07, 2007 10:15 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Interesting... I always seem to use something that isn't universal :(

I was thinking of using awk instead of grep there. May look into it... For the freespace, you could use 'df' to check space of the whole disk.


Top
 Profile YIM  
 PostPosted: Sun Dec 09, 2007 6:35 am   

Joined: Mon Jun 04, 2007 1:43 pm
Posts: 3
back to the problem after some months
i discovered that the grep -P is not available in the ubuntu distro's :?
so i founded a pcregrep packet that replace the grep -P:
a little renaming (with another batch script) of the space with the _
and:
all ok!!! super!!!
thanks!


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP