Advertisement
Hadlock

create random data tree

Aug 8th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.47 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # Usage:
  3. #   wget -q -O - http://pastebin.com/raw/m9vkWEYB | bash
  4. # or
  5. #   curl -sS http://pastebin.com/raw/m9vkWEYB | bash
  6. #
  7. # http://stackoverflow.com/questions/13400312/linux-create-random-directory-file-hierarchy
  8. # Decimal ASCII codes (see man ascii); added space
  9. AARR=( 32 {48..57} {65..90} {97..122} )
  10. # Array count
  11. aarrcount=${#AARR[@]}
  12. OUTDIR="./blue-lion"
  13. ASCIIONLY=1
  14. DIRDEPTH=5
  15. MAXFIRSTLEVELDIRS=2
  16. MAXDIRCHILDREN=12
  17. MAXDIRNAMELEN=12
  18. MAXFILECHILDREN=5
  19. MAXFILENAMELEN=20
  20. MAXFILESIZE=20
  21. MINDIRNAMELEN=1
  22. MINFILENAMELEN=1
  23. MINDIRCHILDREN=1
  24. MINFILECHILDREN=0
  25. MINFILESIZE=1
  26. FILEEXT=".bin"
  27. VERBOSE=0
  28. get_rand_dirname() {
  29.   if [ "$ASCIIONLY" == "1" ]; then
  30.     for ((i=0; i<$((MINDIRNAMELEN+RANDOM%MAXDIRNAMELEN)); i++)) {
  31.       printf \\$(printf '%03o' ${AARR[RANDOM%aarrcount]});
  32.     }
  33.   else
  34.     cat /dev/urandom | tr -dc '[ -~]' | tr -d '[$></~:`\\]' | head -c$((MINDIRNAMELEN + RANDOM % MAXDIRNAMELEN)) | sed 's/\(["]\)/\\\1/g'
  35.   fi
  36.   #echo -e " " # debug last dirname space
  37. }
  38. get_rand_filename() {
  39.   if [ "$ASCIIONLY" == "1" ]; then
  40.     for ((i=0; i<$((MINFILENAMELEN+RANDOM%MAXFILENAMELEN)); i++)) {
  41.       printf \\$(printf '%03o' ${AARR[RANDOM%aarrcount]});
  42.     }
  43.   else
  44.     # no need to escape double quotes for filename
  45.     cat /dev/urandom | tr -dc '[ -~]' | tr -d '[$></~:`\\]' | head -c$((MINFILENAMELEN + RANDOM % MAXFILENAMELEN)) #| sed 's/\(["]\)/\\\1/g'
  46.   fi
  47.   printf "%s" $FILEEXT
  48. }
  49.   echo "Removing old outdir $OUTDIR"
  50.   rm -rf "$OUTDIR"
  51. mkdir "$OUTDIR"
  52. if [ $MAXFIRSTLEVELDIRS -gt 0 ]; then
  53.   NUMFIRSTLEVELDIRS=$((1+RANDOM%MAXFIRSTLEVELDIRS))
  54. else
  55.   NUMFIRSTLEVELDIRS=0
  56. fi
  57. # create directories
  58. for (( ifl=0;ifl<$((NUMFIRSTLEVELDIRS));ifl++ )) {
  59.   FLDIR="$(get_rand_dirname)"
  60.   FLCHILDREN="";
  61.   for (( ird=0;ird<$((DIRDEPTH-1));ird++ )) {
  62.     DIRCHILDREN=""; MOREDC=0;
  63.     for ((idc=0; idc<$((MINDIRCHILDREN+RANDOM%MAXDIRCHILDREN)); idc++)) {
  64.       CDIR="$(get_rand_dirname)" ;
  65.       # make sure comma is last, so brace expansion works even for 1 element? that can mess with expansion math, though
  66.       if [ "$DIRCHILDREN" == "" ]; then DIRCHILDREN="\"$CDIR\"" ;
  67.       else DIRCHILDREN="$DIRCHILDREN,\"$CDIR\"" ; MOREDC=1 ; fi
  68.     }
  69.     if [ "$MOREDC" == "1" ] ; then
  70.       if [ "$FLCHILDREN" == "" ]; then FLCHILDREN="{$DIRCHILDREN}" ;
  71.       else FLCHILDREN="$FLCHILDREN/{$DIRCHILDREN}" ; fi
  72.     else
  73.       if [ "$FLCHILDREN" == "" ]; then FLCHILDREN="$DIRCHILDREN" ;
  74.       else FLCHILDREN="$FLCHILDREN/$DIRCHILDREN" ; fi
  75.     fi
  76.   }
  77.   DIRCMD="mkdir -p $OUTDIR/\"$FLDIR\"/$FLCHILDREN"
  78.   eval "$DIRCMD"
  79.   echo "$DIRCMD"
  80. }
  81. # now loop through all directories, create random files inside
  82. # note printf '%q' escapes to preserve spaces; also here
  83. # escape, and don't wrap path parts in double quotes (e.g. | sed 's_/_"/"_g');
  84. # note then we STILL have to eval to use it!
  85. # but now ls "$D" works, so noneed for QD
  86. # unfortunately backslashes can make '%q' barf - prevent them
  87. find "$OUTDIR" -type d | while IFS= read D ; do
  88.   QD="$(printf '%q' "$(echo "$D")" )" ;
  89.  [ "$VERBOSE" == "1" ] && echo "$D"; #echo "$QD"; ls -la "$D"; #eval "ls -la $QD";
  90.  for ((ifc=0; ifc<$((MINFILECHILDREN+RANDOM%MAXFILECHILDREN)); ifc++)) {
  91.    CFILE="$(get_rand_filename)" ;
  92.    echo -n '> '
  93.    [ "$VERBOSE" == "1" ] && echo "$D"/"$CFILE"
  94.    cat /dev/urandom \
  95.    | head -c$((MINFILESIZE + RANDOM % MAXFILESIZE)) \
  96.    > "$D"/"$CFILE"
  97.  }
  98. done
  99. #echo
  100. #tree -a --dirsfirst -s "$OUTDIR"
  101. #echo "total bytes: $(du -bs $(echo "$OUTDIR"))"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement