Skip to content

Instantly share code, notes, and snippets.

@timothycarambat
Created January 25, 2019 21:38
Show Gist options
  • Save timothycarambat/de45e1727dc31e70759601b591f6b2da to your computer and use it in GitHub Desktop.
Save timothycarambat/de45e1727dc31e70759601b591f6b2da to your computer and use it in GitHub Desktop.
#!/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