With the zsh shell:
mv -- *(oe['REPLY=$RANDOM'][1,10]) /path/to/destination/
Where we use the oe
glob qualifier to o
rder the glob expansion based on the e
valuation of the given code (which here returns a random value), and select the first 10.
On recent GNU systems, and with a shell with support for ksh-style process substitution (ksh93, zsh, bash) you can do:
xargs -r0a <(ls -U --zero | shuf -zn10) mv -t /path/to/destination --
ls -U --zero
can be replaced with printf '%s\0' *
is GNU ls
is too old to support --zero
. With the difference that if there's no non-hidden file in the current directory, you'll get an error about the failure to move a file called *
.
xargs -r0a <(shuf -zen10 -- *) mv -t /path/to/destination --
Could also be used as a more correct/reliable/efficient variation on @boechat107's answer, though with the added caveat that you may run into a Argument list too long error upon attempting to execute shuf
if there's a large number of non-hidden files in the current working directory. The printf
-based approach above should be fine in that regard as printf
is generally built in the shells and so not affected by that limitation of the execve()
system call.
To move 10% as opposed to 10, with zsh:
files=(*(Noe['REPLY=$RANDOM']))
mv -- $files[1,$#files/10] /path/to/destination/
(10% rounded down).