Linux: create random directory/file hierarchy

前端 未结 4 1428
[愿得一人]
[愿得一人] 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:35

    I wasn't too happy with the given answers, so I came up with my own. The following takes my input files and uses /dev/urandom to gather 10 to 256 printable chars, puts in a few more directory separators, creates the directory hierarchy and places a file in it.

    Using urandom creates some really weird directory names which is good for my purpose. I'm sure a real Unix guru could simplify this even more. The dir building could probably be done in a single awk command for example.

    #!/bin/bash
    INDIR='files';
    
    IFS=$'\n'
    for FILE in `ls $INDIR/*`; do
        DIR=`cat /dev/urandom | \
             tr -dc '[ -~]' | \
             tr 'ABCDEF\\\\' '///////' | \
             head -c$((10 + $RANDOM % 256))`
    
        mkdir -p $DIR
        cp $FILE $DIR
    done
    

提交回复
热议问题