Skip to content

Instantly share code, notes, and snippets.

@jimbuck
Created December 19, 2014 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimbuck/d271abca341afd743e81 to your computer and use it in GitHub Desktop.
Save jimbuck/d271abca341afd743e81 to your computer and use it in GitHub Desktop.
Bulk File Generator
#!/usr/bin/env bash
barWidth=60 # constant
filePadding=8 # constant
extension="file" # constant
count=$1
size=$2
resetFlag=$3
if [[ $* == *--help* || $* == *-h* || $# -eq 0 ]]
then
echo
echo "Usage:"
echo
echo " filegen [-h, --help]"
echo " filegen <count> <size> [options]"
echo
echo "Options:"
echo " -r, --reset : Deletes the currently existing files,"
echo
echo "Examples:"
echo
echo " filegen 15000 20k"
echo " (15,000 files, each 20KB)"
echo
echo " filegen 30 150m"
echo " (30 files, each 150MB)"
echo
echo " filegen 6 1g"
echo " (6 files, each 1GB)"
echo
echo "Run 'man mkfile' for more information on creating files."
echo
exit 0
fi
if [ "$count" -le "0" ]
then
echo Count must be greater than zero.
exit 1;
fi
if [[ $resetFlag == "-r" || $resetFlag == "--reset" ]]
then
echo Deleting previous files...
rm -f *0.$extension
rm -f *1.$extension
rm -f *2.$extension
rm -f *3.$extension
rm -f *4.$extension
rm -f *5.$extension
rm -f *6.$extension
rm -f *7.$extension
rm -f *8.$extension
rm -f *9.$extension
fi
function printBar ()
{
index=$1
totalBars=$2
name=$3
percentComplete=$(expr $(($index * 100)) / $totalBars)
barsDone=$(expr $(($index * $barWidth)) / $totalBars)
barsLeft=$(($barWidth - $barsDone))
[[ $barsDone -gt 0 ]] && barDone=$(printf "="'%.s' $(eval "echo {1.."$(($barsDone))"}"))
[[ $barsDone -eq 0 ]] && barDone=""
[[ $barsLeft -gt 0 ]] && barLeft=$(printf "."'%.s' $(eval "echo {0.."$(($barsLeft))"}"))
[[ $barsLeft -eq 0 ]] && barLeft=""
echo -ne "[$barDone$barLeft] $name ($percentComplete%) \r"
}
echo Creating $count files each with a size of $size...
for i in `seq 1 $(expr ${count} + 0)`; do
filename=$(printf "%0${filePadding}d.${extension}" $i)
[ -f "${filename}" ] || mkfile -n $size $filename
printBar $i $count $filename
done
printBar $count $count "Complete!"
echo -ne '\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment