Marlo Guthrie Marlo Guthrie - 1 year ago 118
Bash Question

How can I select random files from a directory in bash?

I have a directory with about 2000 files. How can I select a random sample of

files through using either a bash script or a list of piped commands?

Answer Source

Here's a script that uses GNU sort's random option:

ls |sort -R |tail -$N |while read file; do
    # Something involving $file, or you can leave
    # off the while to just get the filenames
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download