1

My laptop HD is a 120GB SSD. I also have a TB external HD which I used to store my music, photos and videos. I like having some music on my laptop in case I want to listen to something and I don't have Internet access (I don't usually carry around my external HD). But if I had my entire music collection on my laptop it would take up a huge chunk of my free space. I wanted a tool that got a random selection of albums from my external HD and transfered them to my music collection on my laptop. For the transfer I would like to specify only the maximum amount of data I want to transfer. For example, I would like to be able to specify, get me 10GB worth of random albums from the music collection in my external HD and put them in my laptop's music directory while eliminating what had previously been available on my laptop's music directory. Is there a tool that can help me achieve this? This would also be useful for transferring music to my smart-phone. P.S. I'm an Ubuntu user.

1

As long as you don't need exactly 10GB of data, just approximately 10GB but no more, you can write a bash function that does this. Add these lines to your $HOME/.bashrc file:

function random_copy(){
    size=0; 
    tries=0;
    dirs=(); 
  ## The maximum size, change this to whatever you want
    limit=10000000
  ## The maximum number of times we will try to get 
  ## another dir if the current one is too big.
    maxtries=20
  ## While we have not reached the size limit
    while [[ $size -lt $limit ]] ; do 
  ## Find a random, non-empty directory from the source. 
  ## sort -R does a  random sort and head -n 1 prints the first line.
    dir="$(find $1/*/  -not -empty -type d | sort -R | head -n 1)"
  ## Get this dir's size
    dsize=$(du -s "$dir" | cut -f 1)
  ## If this dir does not make us pass the limit
    if [[  $((size + $dsize)) -le $limit ]]; then
        echo "Copying $dir" 1>&2
    ## Copy it to target
        cp -r "$dir" $2
    ## Add its size to $size
        let size+=$dsize
  ## If this dir makes us pass the limit
  ## try $maxtries times to find another that does not
    else
        let tries++;
        if [[ $tries -gt $maxtries ]]; then
        echo "" 1>&2;
        echo "Final size =  $size"
        break;
        fi
    fi
    done; 
}

You can then call the function by running

random_copy /path/to/source /path/to/destination

For example

random_copy ~/Music /tmp/music

NOTES

  • The function will look for folders that are under the source directory. I am assuming that each of your albums is in its own folder.
  • The actual size you end up with will be different every time you run the function. If a particular directory makes it pass the size limit, it will try again until $maxtries is reached. You can control this by changing maxtries=20 to whatever you want.
  • Thank you! I figured I would probably just end up doing a Bash script. This is a pretty nice head start, thanks. – tatsuhirosatou Jul 13 '13 at 20:31
1

Use the tool Random Copy.

Random Copy has three Modes:

  • A file-based mode: the normal Mode for create a Music collection out of different Source Folders.

  • A directory-based Mode: to create a Music collection, also based on different source directories, but in different to the file-based Mode the complete Content of a directory is copied.

  • A CD-based Mode: A mode special to fit the size of a CD or DVD with songs, so there is less space left. Destination files could be the source files or wave files after internal conversion.

enter image description here

Alternative Linux

One option would be for Linux Mix2Stix.

Steps for installation and how to use, it described on page.

enter image description here

Note: Mix2Stix requires Java which has to be installed on the system. The source is included in the distribution.


Using the find/cp command

find -L ~ -type f -name "*.mp3" | sort -R | tail -n50 | while read file; do cp "$file" /media/<yourdisk>; done

The find command will search for mp3 files in your user folder ~. -n50 represents how many files are going to be copied.

  • That looks great, do you know of a Linux alternative? – tatsuhirosatou Jul 13 '13 at 18:15
  • @climatewarrior: Updated my answer. I will try to create a script that does it. – stderr Jul 13 '13 at 18:36

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.