Linux: create random directory/file hierarchy

前端 未结 4 1422
[愿得一人]
[愿得一人] 2020-12-28 23:52

For testing a tool I need a directory with a whole bunch of different Office files in a deep nested structure. I already have the files in a directory, but now need to creat

4条回答
  •  一向
    一向 (楼主)
    2020-12-29 00:30

    Thanks to all who posted here; it turns out, it wasn't really trivial to escape filenames with special characters, so I built my own script based on those here; here is how it behaves with special character filenames:

    $ ~/rndtree.sh ./rndpath 0 3 1
    Warning: will create random tree at: ./rndpath
    Proceed (y/n)? y
    Removing old outdir ./rndpath
    mkdir -p ./rndpath/";"/{")?DxVBBJ{w2","L,|+","^VC)Vn.6!"}/"D+,IFJ( LN"
    > > > > > > > > > > > 
    ./rndpath
    └── [       4096]  ;
        ├── [       4096]  )?DxVBBJ{w2
        │   ├── [       4096]  D+,IFJ( LN
        │   │   └── [        929]  r2.bin
        │   ├── [       8557]  %3fsaG# Rl;ffXf.bin
        │   └── [      19945]  Dzk .bin
        ├── [       4096]  L,|+
        │   ├── [       4096]  D+,IFJ( LN
        │   │   ├── [       2325]  6Qg#pe5j'&ji49oqrO.bin
        │   │   ├── [      16345]  #?.bin
        │   │   └── [       2057]  Uz-0XtLVWz#}0lI.bin
        │   ├── [       2543]  bbtA-^s22vdTu.bin
        │   └── [      10848]  K46+kh7L9.bin
        ├── [       4096]  ^VC)Vn.6!
        │   ├── [       4096]  D+,IFJ( LN
        │   ├── [      10502]  8yY,MqZ ^5+_SA^.r4{.bin
        │   └── [      17628]  ipO"|69.bin
        └── [      12376]  a2Y% }G1.qDir.bin
    
    7 directories, 11 files
    total bytes: 136823 ./rndpath
    

    and here with a safe subset of ASCII:

    $ ~/rndtree.sh ./rndpath 1 3 1
    Warning: will create random tree at: ./rndpath
    Proceed (y/n)? y
    Removing old outdir ./rndpath
    mkdir -p ./rndpath/"48SLS"/{"nyG","jIC6vj"}/{"PSLd5tMn","V R"}
    > > > > > > > 
    ./rndpath
    ├── [       4096]  48SLS
    │   ├── [       4096]  jIC6vj
    │   │   ├── [       4096]  PSLd5tMn
    │   │   ├── [       4096]  V R
    │   │   │   ├── [        922]  lg.bin
    │   │   │   └── [       9600]  VVYG.bin
    │   │   ├── [      10883]  B7nt06p.bin
    │   │   └── [      19339]  g5uT i.bin
    │   ├── [       4096]  nyG
    │   │   ├── [       4096]  PSLd5tMn
    │   │   └── [       4096]  V R
    │   │       └── [       6128]  1tfLR.bin
    │   └── [       5448]  Jda.bin
    └── [      18196]  KwEXu2O2H9s.bin
    

    Spaces should be handled in both cases - however, note that subdirectory names repeat (while filenames do not).

    The script rndtree.sh:

    #!/usr/bin/env bash
    
    # http://stackoverflow.com/questions/13400312/linux-create-random-directory-file-hierarchy
    # Decimal ASCII codes (see man ascii); added space
    AARR=( 32 {48..57} {65..90} {97..122} )
    # Array count
    aarrcount=${#AARR[@]}
    
    if [ "$1" == "" ] ; then
      OUTDIR="./rndpath" ;
    else
      OUTDIR="$1" ;
    fi
    
    if [ "$2" != "" ] ; then
      ASCIIONLY="$2" ;
    else
      ASCIIONLY=1 ;
    fi
    
    if [ "$3" != "" ] ; then
      DIRDEPTH="$3" ;
    else
      DIRDEPTH=3 ;
    fi
    
    if [ "$4" != "" ] ; then
      MAXFIRSTLEVELDIRS="$4" ;
    else
      MAXFIRSTLEVELDIRS=2 ;
    fi
    
    if [ "$5" != "" ] ; then
      MAXDIRCHILDREN="$5" ;
    else
      MAXDIRCHILDREN=4 ;
    fi
    
    if [ "$6" != "" ] ; then
      MAXDIRNAMELEN="$6" ;
    else
      MAXDIRNAMELEN=12 ;
    fi
    
    if [ "$7" != "" ] ; then
      MAXFILECHILDREN="$7" ;
    else
      MAXFILECHILDREN=4 ;
    fi
    
    if [ "$8" != "" ] ; then
      MAXFILENAMELEN="$8" ;
    else
      MAXFILENAMELEN=20 ;
    fi
    
    if [ "$9" != "" ] ; then
      MAXFILESIZE="$9" ;
    else
      MAXFILESIZE=20000 ;
    fi
    
    MINDIRNAMELEN=1
    MINFILENAMELEN=1
    MINDIRCHILDREN=1
    MINFILECHILDREN=0
    MINFILESIZE=1
    FILEEXT=".bin"
    VERBOSE=0 #1
    
    get_rand_dirname() {
      if [ "$ASCIIONLY" == "1" ]; then
        for ((i=0; i<$((MINDIRNAMELEN+RANDOM%MAXDIRNAMELEN)); i++)) {
          printf \\$(printf '%03o' ${AARR[RANDOM%aarrcount]});
        }
      else
        cat /dev/urandom | tr -dc '[ -~]' | tr -d '[$> '
        [ "$VERBOSE" == "1" ] && echo "$D"/"$CFILE"
        cat /dev/urandom \
        | head -c$((MINFILESIZE + RANDOM % MAXFILESIZE)) \
        > "$D"/"$CFILE"
      }
    done
    
    echo
    tree -a --dirsfirst -s "$OUTDIR"
    echo "total bytes: $(du -bs $(echo "$OUTDIR"))"
    

提交回复
热议问题