Search code examples
bashunixsolaris-10

move files to a few directories in random order


I would like to move a lot of files from one directory to a few directories dst/f0..dst/f9 in random order.

I tried to do it like this:

ls src/*.xml | head |  xargs -I {} mv {}  f$(($RANDOM % 10))

but all files has been moved into one directory src/fN

How I can to do it? Thanks


Solution

  • This is fastest solution based on the @fedorqui solution. Thanks @fedorqui!

    ls -1 -f | /usr/xpg4/bin/awk -v seed=$RANDOM 'BEGIN{srand(seed)} /\.xml$/ {printf "mv \"%s\" dst/f%d\n", $1, int(rand()*10)}' | sh
    

    I measured speed for this script. It works fast

    date;ls -1 -f | /usr/xpg4/bin/awk -v seed=$RANDOM 'BEGIN{srand(seed)} /\.xml$/ {printf "mv \"%s\" dst/f%d\n", $1, int(rand()*10)}' > list.sh;date;cat list.sh | wc -l
    

    Output for the script

    Fri Jul 17 13:21:54 YEKT 2015
    Fri Jul 17 13:21:54 YEKT 2015
    29142
    

    I understand parsing the output of ls maybe is not safe. But I don't need a portable solutions in this particular case