How to move a given number of random files on Unix/Linux OS How to move a given number of random files on Unix/Linux OS linux linux

How to move a given number of random files on Unix/Linux OS


Use a combination of shuf and xargs (it's a good idea to look at their documentation with man):

shuf -n 10 -e * | xargs -i mv {} path-to-new-folder

The command above selects 10 random files of the current folder (the * part) and then move them to the new folder.

Update

Although longer, one might find this version even simpler to understand:

ls | shuf -n 10 | xargs -i mv {} path-to-new-folder

shuf just generates a random permutation of the standard input, limiting the results to 10 (like using head, but probably faster).


You could use bash random generator that generates an int between 0 and 32767 to choose if a file must be put in set1 or set2. That would do:

for file in ./*; do  val=$RANDOM  if test $val -gt 3276; then    mv "$file" ../set1  else    mv "$file" ../set2  fidone


Alternative version with find to avoid problems with folders.It copies 31415 randomly chosen files into /home/user/dir/

find . -maxdepth 1 -type f | sort -R | head -31415 | xargs cp -t /home/user/dir/