{"id":391,"date":"2011-03-13T21:06:06","date_gmt":"2011-03-13T20:06:06","guid":{"rendered":"http:\/\/trigonakis.com\/blog\/?p=391"},"modified":"2011-04-09T19:32:19","modified_gmt":"2011-04-09T18:32:19","slug":"selecting-a-random-filefolder-of-a-folder-in-linux","status":"publish","type":"post","link":"http:\/\/trigonakis.com\/blog\/2011\/03\/13\/selecting-a-random-filefolder-of-a-folder-in-linux\/","title":{"rendered":"Selecting a random file\/folder of a folder in Linux"},"content":{"rendered":"<p>I always had the following problem: &#8220;<strong>How to select which movie (from the ones in my collection) to watch<\/strong>&#8220;. In the past, I had used the random (pseudo-random to be precise) capabilities of C, Java, Erlang, and Javascript to select the movie.<\/p>\n<p>Today I wrote a simple script that <strong>selects a random content (file or folder) within a folder<\/strong>:<\/p>\n<pre lang=\"bash\">\r\n#! \/bin\/sh\r\n\r\nif [ $# -gt 0 ]; then\r\n    cd \"$1\";\r\nfi\r\n\r\nls -1 | awk 'BEGIN {srand()}\r\n\t{x[NR] = $0}\r\n\tEND {print \"Selected\", x[1 + int(rand() * NR)]}'\r\n<\/pre>\n<p>Save it in a file (lets say <code>srandom<\/code>, make it executable:<\/p>\n<pre lang=\"bash\">\r\n$ chmod +x srandom\r\n<\/pre>\n<p>and execute it either with 0 or 1 arguments. Without an argument, the selection is done from the contents of the <code>srandom<\/code>&#8216;s container, else the path given as an argument is used.<\/p>\n<pre lang=\"bash\">\r\n$ .\/srandom.sh\r\nSelected lrandom.html\r\n$ .\/srandom.sh \/bin\r\nSelected gzexe\r\n<\/pre>\n<h3>Update <\/h3>\n<p>A much more elegant solution:<\/p>\n<pre lang=\"bash\">\r\n$ ls -1 | shuf -n1\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I always had the following problem: &#8220;How to select which movie (from the ones in my collection) to watch&#8220;. In the past, I had used the random (pseudo-random to be precise) capabilities of C, Java, Erlang, and Javascript to select the movie. Today I wrote a simple script that selects a random content (file or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[37],"tags":[53,10,9,54],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ouW6-6j","_links":{"self":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/391"}],"collection":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/comments?post=391"}],"version-history":[{"count":6,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/391\/revisions"}],"predecessor-version":[{"id":394,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/391\/revisions\/394"}],"wp:attachment":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/media?parent=391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/categories?post=391"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/tags?post=391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}