tag:blogger.com,1999:blog-12753102.post7339846590597217955..comments2024-03-28T15:41:37.170-04:00Comments on Ben's Journal: Bash Shell Hack: Picking a random set of files from a directoryBen Simonhttp://www.blogger.com/profile/09833753747177544979noreply@blogger.comBlogger15125tag:blogger.com,1999:blog-12753102.post-25833846917177288402014-01-23T12:42:36.302-05:002014-01-23T12:42:36.302-05:00What about the easy way?
find -type f | shuf -n1What about the easy way?<br /><br />find -type f | shuf -n1ladikonoreply@blogger.comtag:blogger.com,1999:blog-12753102.post-19882246428309421152013-01-15T19:48:26.877-05:002013-01-15T19:48:26.877-05:00yes:
ls | sort -R | head -15 | while read file
do
...yes:<br />ls | sort -R | head -15 | while read file<br />do<br />cp $file /tmp/<br />done<br /><br />Just replace /tmp/ with which ever folder you want to copy to.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-12753102.post-87287008661207659072013-01-15T19:46:56.837-05:002013-01-15T19:46:56.837-05:00you can do what you like by using a while read e.g...you can do what you like by using a while read e.g.<br />ls | sort -R | head -15 | while read file<br />do<br />cat $file<br />done<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-12753102.post-43999657004288816192012-03-02T07:01:49.436-05:002012-03-02T07:01:49.436-05:00Just do it more efficiently by omitting `expr $RAN...Just do it more efficiently by omitting `expr $RANDOM % 1000` which gives no advantage:<br /><br />ls | while read x; do echo "$RANDOM:$x"; done | sort -n|cut -d':' -f2| head -15<br /><br />time results with just "$RANDOM:$x"<br />real 0m0.04s<br />user 0m0.04s<br />sys 0m0.05s<br /><br />time results with "`expr $RANDOM % 1000`:$x":<br />real 0m1.85s<br />user 0m0.50s<br />sys 0m1.32sDieter Lohrstraeterhttp://www.adbc.denoreply@blogger.comtag:blogger.com,1999:blog-12753102.post-85420963582121929612011-11-01T17:11:22.218-04:002011-11-01T17:11:22.218-04:00Great. This is works like a gem - Is it possible t...Great. This is works like a gem - Is it possible to also copy the file name shown on $x variable to a different folder? if so how?Raamkumnoreply@blogger.comtag:blogger.com,1999:blog-12753102.post-53933796702787939182011-08-19T11:44:35.915-04:002011-08-19T11:44:35.915-04:00How would one then have the script open the output...How would one then have the script open the output? Thanks for your help.<br /><br />EvanEvan Rosehttp://evanrose.comnoreply@blogger.comtag:blogger.com,1999:blog-12753102.post-53002189056769190982011-07-28T04:06:50.875-04:002011-07-28T04:06:50.875-04:00ls | sort -R | head -15ls | sort -R | head -15simonhttps://www.blogger.com/profile/07800779073409671908noreply@blogger.comtag:blogger.com,1999:blog-12753102.post-88597697240584333972011-07-28T04:06:10.563-04:002011-07-28T04:06:10.563-04:00ls | sort -R | head -1ls | sort -R | head -1simonhttps://www.blogger.com/profile/07800779073409671908noreply@blogger.comtag:blogger.com,1999:blog-12753102.post-89069452166615455682011-04-12T11:30:19.444-04:002011-04-12T11:30:19.444-04:00When using in something like a 'find' comm...When using in something like a 'find' command, it helps to use 'expr' as such:<br /><br />find /home/ -name "*.mp3" -exec \<br /> sh -c 'echo `expr $RANDOM` {}' ";" \<br /> | sort -g\<br /> | awk '{print $2}'Michael Babcockhttps://www.blogger.com/profile/17706672245403460737noreply@blogger.comtag:blogger.com,1999:blog-12753102.post-77993194731308668392011-03-27T19:49:52.013-04:002011-03-27T19:49:52.013-04:00Just found out about this (does same thing):
ls |...Just found out about this (does same thing):<br /><br />ls |sort -R | head -n5Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-12753102.post-87085561250002889092010-11-02T18:42:20.144-04:002010-11-02T18:42:20.144-04:00That's pretty elegant. ThanksThat's pretty elegant. ThanksAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-12753102.post-45713103755286932152010-04-09T15:12:53.774-04:002010-04-09T15:12:53.774-04:00I always forget about cut -- that's a good poi...I always forget about cut -- that's a good point.<br /><br />-BenBen Simonhttps://www.blogger.com/profile/09833753747177544979noreply@blogger.comtag:blogger.com,1999:blog-12753102.post-39332933222420798612010-04-09T13:42:37.815-04:002010-04-09T13:42:37.815-04:00sed definitely works in this instance, but perhaps...sed definitely works in this instance, but perhaps using cut would be more elegant?<br /><br />eg:<br />cut -d':' -f2-<br /><br />Just my two cents; in the end, as long as it works, it's all good.<br /><br />Thanks for this; it's definitely helping!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-12753102.post-26548040265084850452009-09-23T16:09:06.100-04:002009-09-23T16:09:06.100-04:00it works. thanks!it works. thanks!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-12753102.post-26571530501817484802009-05-19T10:10:00.000-04:002009-05-19T10:10:00.000-04:00creative script, and a nice photo of you and your ...creative script, and a nice photo of you and your girlfriend by the way. I used in my script a trick I read somewhere else: the -R option of GNU sort does a random sort. But that option is not in the POSIX specification of sort on unix.orgRichardnoreply@blogger.com