copying random sample of files

Hi everyone I was just wondering how I can copy a random subset of files from one folder to another. And, of course, there bash utilities for that... ;-) I came across two solutions for copying 20 files from the current directory into DEST: - shuf cp `ls | shuf -n 20` DEST - sort (using random sort) ls | sort -R | tail -20 | while read file do cp $file DEST done source: https://stackoverflow.com/questions/414164/ Man pages: - shuf: https://www.man7.org/linux/man-pages/man1/shuf.1.html - sort: https://man7.org/linux/man-pages/man1/sort.1.html Cheers, Peter

That's the power of Linux/Bash for you, but out of curiosity, why would you want to do that? You can enhance by ensuring your source objects are not directories too unless you're sure that'll never happen by checking before hand or using the -a option. cp will fail otherwise. On 12/09/24 14:59, Peter Reutemann wrote:
I was just wondering how I can copy a random subset of files from one folder to another. And, of course, there bash utilities for that... ;-)
I came across two solutions for copying 20 files from the current directory into DEST: - shuf cp `ls | shuf -n 20` DEST
- sort (using random sort) ls | sort -R | tail -20 | while read file do cp $file DEST done
source: https://stackoverflow.com/questions/414164/ -- Mark Smith E: mark(a)winksmith.com

That's the power of Linux/Bash for you, but out of curiosity, why would you want to do that?
Was just testing a deep learning model and needed to push a small subset of images through (the input directory contained several thousand).
You can enhance by ensuring your source objects are not directories too unless you're sure that'll never happen by checking before hand or using the -a option. cp will fail otherwise.
Good spotting! My input directory contained only images, so it wasn't an issue for me. Cheers, Peter

I tend to use shuf For example I haveĀ a cron job at 22:30 daily that takes 30 random lines from "/var/lib/mpd/playlists/Bed time.m3u" and adds them to the mpd queue. Wayne -------- Original Message --------
Hi everyone
I was just wondering how I can copy a random subset of files from one folder to another. And, of course, there bash utilities for that... ;-)
participants (3)
-
Mark Smith
-
Peter Reutemann
-
Wayne Rooney