Created
January 25, 2019 21:38
-
-
Save timothycarambat/de45e1727dc31e70759601b591f6b2da to your computer and use it in GitHub Desktop.
A Random file picker made for /u/Sr_Navarre for request https://www.reddit.com/r/programmingrequests/comments/ahyb10/random_file_picker_for_mac_that_supports/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#This file will take two inputs. One origin directory with N subfolders | |
#Another Directory which to post the Copied files to | |
#a Count as a number | |
#Will output in Target Directory a random selection of files. | |
#you may have to give permissions to this file to work via `chmod 755 randomizer.sh` | |
#You can execute the script by ./randomizer.sh '/home/me/Desktop/Files' '/home/me/Desktop/Destination' 5 2>/dev/null | |
#You will see some printed output. You can open the directory and the files will be present. | |
#If you specifiy a higher count than files that exist you will have to force kill the script. | |
FILE_LOC=$1 | |
DEST_LOC=$2 | |
COUNT=$3 | |
echo "Getting ${COUNT} Files" | |
count_adj=$((COUNT+1)) | |
folders= | |
files= | |
selections= | |
for d in $(find $FILE_LOC/* -maxdepth 1 -type d) | |
do | |
folders+=("$d") | |
done | |
for folder in "${folders[@]}" | |
do | |
for file in $(find $folder/ -maxdepth 1 -type f) | |
do | |
files+=("$file") | |
done | |
done | |
while [ "${#selections[@]}" -lt "${count_adj}" ] | |
do | |
rand=$[$RANDOM % ${#files[@]}] | |
selections+=("${files[$rand]}") | |
eval selections=($(for i in "${selections[@]}" ; do echo "\"$i\"" ; done | sort -u)) | |
done | |
for file in "${selections[@]}" | |
do | |
echo "Copying ${file} to ${DEST_LOC}" | |
cp $file $DEST_LOC | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment