P
P
Printip2017-09-06 17:05:11
linux
Printip, 2017-09-06 17:05:11

How to select random files from a folder?

There is a folder with a million files in it. You need to select random from a folder of 100 thousand files and copy them to another folder.
Can you please tell me how it can be done in the terminal?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vladimir Mukovoz, 2017-09-06
@Printip

Like this

#!/bin/bash
if ! [ $3 ];then
  precent=6
else
  precent=$3
fi
ls $1 > ls.log
while read line
do
  [ $[ $RANDOM % $precent ] == 0 ] && cp -R $1/$line $2
done < ls.log

You save this script for example here /usr/local/bin , for example under the name cprandom,
then you make it executable.
chmod +x /usr/local/bin/cprandom
then you work
cprandom $1 $2 $3
where
$1 is the address of the folder with your files
$2 - the address of the folder where to copy the random ones
$3 - indicate the number of digits starting from 0 that participate in the random, that is, specifying 1 there will always be 0 and all files will be copied, 2 - 50 by 50, 3, a third, and so on. The default value is 6
That is, for example
, cprandom /home/vity /tmp 20
Enjoy using it))
I wrote on my knee, I didn’t even run it once, but it seems without errors. If anything write)

D
Derevyanko Alexander, 2017-09-06
@dio4

And you can write a script in bash, especially since the question section
is "BASH LINUX TERMINAL" :)
Using a random number generator in bash. For example, shuf -i 100000-999999 -n 100000
this will generate 100000 sl. numbers in the range from 100000 to 999999, which can be used to randomly select the files you need in a loop.

A
Adamos, 2017-09-06
@Adamos

ls | shuf | tail | mv
Something like that. Collect the keys yourself according to man-s.

A
Anton, 2017-09-06
Reytarovsky @Antonchik

You can write a python script

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question