Advertisement
Hadlock

create random data tree 2

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