Skip to content

Instantly share code, notes, and snippets.

@krisgesling
Created June 2, 2020 23:42
Show Gist options
  • Save krisgesling/afd6ce3d22d8c1bda1b9aaa1256122d6 to your computer and use it in GitHub Desktop.
Save krisgesling/afd6ce3d22d8c1bda1b9aaa1256122d6 to your computer and use it in GitHub Desktop.
Move a defined proportion of files from one folder to another. Particularly useful for directories with large numbers of files.
#!/bin/bash
DIR_FROM=$1
DIR_TO=$2
PERCENTAGE=$3
COUNTER=0
for file in $DIR_FROM*; do
val=$RANDOM
# Max possible value is 32760
((split=32760*$PERCENTAGE/100))
if (($val < $split)); then
echo "Moving: $file"
mv "$file" $DIR_TO
((COUNTER=$COUNTER+1))
fi
done
echo "Moved $COUNTER files."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment