The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #3 (permalink)  
Old 11-21-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
Here is a cool trick that I'm borrowing from cfajohnson... First put a random number in front of each file name:

ls | awk 'BEGIN {srand()} {printf "%.0f %s \n",rand()*99999, $0; }'

then sort it. This will put the lines in a random order. Then just pick the first few lines:

ls | awk 'BEGIN {srand()} {printf "%.0f %s \n",rand()*99999, $0; }' |sort -n | head -2 | awk '{print $2}'