Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

random

I have a directory with about 2000 files. How can I select a random sample of N files through using either a bash script or a list of piped commands?

like image 532
Marlo Guthrie Avatar asked Jan 05 '09 19:01

Marlo Guthrie


People also ask

How do I select random files in a folder?

As a test, open Explorer and browse to any of your user folders with a lot of content: Documents, Music, Pictures, whatever. Right-click the folder in the left-hand pane — not the right — and click the new “Select Random” option.

How do I get a list of files in a directory in bash?

To see a list of all subdirectories and files within your current working directory, use the command ls . In the example above, ls printed the contents of the home directory which contains the subdirectories called documents and downloads and the files called addresses.


2 Answers

You can use shuf (from the GNU coreutils package) for that. Just feed it a list of file names and ask it to return the first line from a random permutation:

ls dirname | shuf -n 1 # probably faster and more flexible: find dirname -type f | shuf -n 1 # etc.. 

Adjust the -n, --head-count=COUNT value to return the number of wanted lines. For example to return 5 random filenames you would use:

find dirname -type f | shuf -n 5 
like image 44
Nordic Mainframe Avatar answered Oct 12 '22 17:10

Nordic Mainframe


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 done 
like image 96
Josh Lee Avatar answered Oct 12 '22 15:10

Josh Lee