Skip to content

Instantly share code, notes, and snippets.

@sgpinkus
Last active June 5, 2021 03:33
Show Gist options
  • Save sgpinkus/b33dcf08d2c5499ff74c to your computer and use it in GitHub Desktop.
Save sgpinkus/b33dcf08d2c5499ff74c to your computer and use it in GitHub Desktop.
#!/bin/bash -e
# Make a balanced file tree with files full of random bytes for testing purposes.
# Script takes three parameters depth, width, and filesize.
DEPTH=2
WIDTH=10
FSIZE=10000
DORANDOM=1
#WRITEPROBABILITY=5
function rec_make_tree() {
local DEPTH=$1
local WIDTH=$2
local FSIZE=$3
local MYDIR=${4:-.}
local i
for (( i=0; $i<$WIDTH; i=$i+1 ))
do
if [[ $DEPTH -gt 0 ]]; then
[[ -d $MYDIR/$i ]] || mkdir $MYDIR/$i
rec_make_tree $(($DEPTH-1)) $WIDTH $FSIZE $MYDIR/$i
else
if [[ ( -z "$DORANDOM" ) || ( $(($RANDOM%${WRITEPROBABILITY:-1})) -eq 0 ) ]]; then
head -c $FSIZE /dev/urandom > $MYDIR/$i
#echo yes
else
true
#echo no
fi
fi
done
}
DEPTH=${1:-$DEPTH}
WIDTH=${2:-$WIDTH}
FSIZE=${3:-$FSIZE}
echo "Making a file tree with DEPTH=$DEPTH WIDTH=$WIDTH FILESIZE=$FSIZE"
rec_make_tree $DEPTH $WIDTH $FSIZE .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment