微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Linux:创build随机目录/文件层次结构

为了testing一个工具,我需要一个包含大量不同的Office文件的深度嵌套结构的目录。 我已经在一个目录中的文件,但现在需要创build一些随机嵌套子目录,并分散在其中的文件

我可以坐下来用我select的编程语言编写一个合适的程序,但是我想知道是否可能有Linux命令行工具+ Bash的聪明组合来实现我想要的。

编辑:澄清,我的input是一个约200个文件的目录。 输出应该是包含这些文件或多或less均匀分布的目录层次结构。 目录名称应该多于单个字母,长度随机变化并使用各种允许的字符(utf-8文件系统)。

运行aws cli时Pyexpat导入错误

Python:如果在阻塞raw_input的情况下退出CLI?

如何使用命令行(CLI)更改Windows 7上的文件文件夹权限?

它可能在windows中安装telegram-cli

如何从常规命令的任何地方做一个Ruby文件(在一个gem)可执行文件

如何运行JavaScript的Windows控制台

2>在Unix命令行上的含义是什么?

linux命令“符号

如何从PHP cli获得Linux控制台$ COLUMNS和$ ROWS?

Linux命令行开关和参数是否有标准?

你可以使用bash brace-expansion :

mkdir -p {a,b}/{e,f,g}/{h,i,j}

├───a │ ├───e │ │ ├───h │ │ ├───i │ │ └───j │ ├───f │ │ ├───h │ │ ├───i │ │ └───j │ └───g │ ├───h │ ├───i │ └───j └───b ├───e │ ├───h │ ├───i │ └───j ├───f │ ├───h │ ├───i │ └───j └───g ├───h ├───i └───j

这是一个生成随机目录结构的脚本:

#!/bin/bash # Decimal ASCII codes (see man ascii) ARR=( {48..57} {65..90} {97..122} ) # Array count arrcount=${#ARR[@]} # return a random string get_rand_dir(){ for ((i=1; i<$((RANDOM%30)); i++)) { printf \$(printf '%03o' ${ARR[RANDOM%arrcount]}); } } dir=/tmp/ # appending random characters to make a hierarchy for ((i=0; i<$((RANDOM%100)); i++)) { dir+="$(get_rand_dir)/" } echo $dir mkdir -p "$dir" oldir=$(echo "$dir" | cut -d '/' -f1-3) while [[ $dir ]]; do dir=${dir%/*} cd $dir for ((i=0; i<$((RANDOM%100)); i++)) { mkdir &>/dev/null -p $(get_rand_dir) } done tree "$oldir"

OUTPUT

/tmp/x ├── egeDVPW ├── iOkr ├── l ├── o1gye8uF ├── q │ ├── 4Dlrfagv │ ├── 4Yxmoqf │ ├── 8LkyIrXA │ ├── 8m9kse8s │ ├── aV │ ├── in │ │ ├── 12zdLso68HWlPK │ │ │ ├── C │ │ │ ├── DOYt8wUW │ │ │ ├── FXP │ │ │ ├── hFLem8 │ │ │ ├── hhHIv │ │ │ ├── iD87kxs54x04 │ │ │ ├── oFM │ │ │ ├── OjFT

现在你可以创建一个dir数组:

shopt -s globstar # require bash4 dirs=( /tmp/x/** ) printf '%sn' ${dirs[@]}

并用文件随机填充dirs。 你有足够的例子来这样做。 我做了最辛苦的工作。

感谢所有在这里发布的人 事实证明,用特殊字符转义文件名并不是很简单,所以我在这里建立了自己的脚本。 这是如何表现与特殊的字符文件名:

$ ~/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

这里有一个安全的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","VR"} > > > > > > > ./rndpath ├── [ 4096] 48SLS │ ├── [ 4096] jIC6vj │ │ ├── [ 4096] PSLd5tMn │ │ ├── [ 4096] VR │ │ │ ├── [ 922] lg.bin │ │ │ └── [ 9600] VVYG.bin │ │ ├── [ 10883] B7nt06p.bin │ │ └── [ 19339] g5uT i.bin │ ├── [ 4096] nyG │ │ ├── [ 4096] PSLd5tMn │ │ └── [ 4096] VR │ │ └── [ 6128] 1tfLR.bin │ └── [ 5448] Jda.bin └── [ 18196] KwEXu2O2H9s.bin

在两种情况下都应该处理空格 – 但是,请注意子目录名称重复(而文件名不重复)。

脚本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 '[$></~:`\]' | head -c$((MINDIRNAMELEN + RANDOM % MAXDIRNAMELEN)) | sed 's/(["])/\1/g' fi #echo -e " " # debug last dirname space } get_rand_filename() { if [ "$ASCIIONLY" == "1" ]; then for ((i=0; i<$((MINFILENAMELEN+RANDOM%MAXFILENAMELEN)); i++)) { printf \$(printf '%03o' ${AARR[RANDOM%aarrcount]}); } else # no need to escape double quotes for filename cat /dev/urandom | tr -dc '[ -~]' | tr -d '[$></~:`\]' | head -c$((MINFILENAMELEN + RANDOM % MAXFILENAMELEN)) #| sed 's/(["])/\1/g' fi printf "%s" $FILEEXT } echo "Warning: will create random tree at: $OUTDIR" [ "$VERBOSE" == "1" ] && echo " MAXFirsTLEVELDirs $MAXFirsTLEVELDirs ASCIIONLY $ASCIIONLY DIRDEPTH $DIRDEPTH MAXDIRCHILDREN $MAXDIRCHILDREN MAXDIRNAMELEN $MAXDIRNAMELEN MAXFILECHILDREN $MAXFILECHILDREN MAXFILENAMELEN $MAXFILENAMELEN MAXFILESIZE $MAXFILESIZE" read -p "Proceed (y/n)? " READANS if [ "$READANS" != "y" ]; then exit fi if [ -d "$OUTDIR" ]; then echo "Removing old outdir $OUTDIR" rm -rf "$OUTDIR" fi mkdir "$OUTDIR" if [ $MAXFirsTLEVELDirs -gt 0 ]; then NUMFirsTLEVELDirs=$((1+RANDOM%MAXFirsTLEVELDirs)) else NUMFirsTLEVELDirs=0 fi # create directories for (( ifl=0;ifl<$((NUMFirsTLEVELDirs));ifL++ )) { FLDIR="$(get_rand_dirname)" FLCHILDREN=""; for (( ird=0;ird<$((DIRDEPTH-1));ird++ )) { DIRCHILDREN=""; MOREDC=0; for ((idc=0; idc<$((MINDIRCHILDREN+RANDOM%MAXDIRCHILDREN)); idc++)) { CDIR="$(get_rand_dirname)" ; # make sure comma is last,so brace expansion works even for 1 element? that can mess with expansion math,though if [ "$DIRCHILDREN" == "" ]; then DIRCHILDREN=""$CDIR"" ; else DIRCHILDREN="$DIRCHILDREN,"$CDIR"" ; MOREDC=1 ; fi } if [ "$MOREDC" == "1" ] ; then if [ "$FLCHILDREN" == "" ]; then FLCHILDREN="{$DIRCHILDREN}" ; else FLCHILDREN="$FLCHILDREN/{$DIRCHILDREN}" ; fi else if [ "$FLCHILDREN" == "" ]; then FLCHILDREN="$DIRCHILDREN" ; else FLCHILDREN="$FLCHILDREN/$DIRCHILDREN" ; fi fi } DIRCMD="mkdir -p $OUTDIR/"$FLDIR"/$FLCHILDREN" eval "$DIRCMD" echo "$DIRCMD" } # Now loop through all directories,create random files inside # note printf '%q' escapes to preserve spaces; also here # escape,and don't wrap path parts in double quotes (eg | sed 's_/_"/"_g'); # note then we STILL have to eval to use it! # but Now ls "$D" works,so noneed for QD # unfortunately backslashes can make '%q' barf - prevent them find "$OUTDIR" -type d | while IFS= read D ; do QD="$(printf '%q' "$(echo "$D")" )" ; [ "$VERBOSE" == "1" ] && echo "$D"; #echo "$QD"; ls -la "$D"; #eval "ls -la $QD"; for ((ifc=0; ifc<$((MINFILECHILDREN+RANDOM%MAXFILECHILDREN)); ifc++)) { CFILE="$(get_rand_filename)" ; echo -n '> ' [ "$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"))"

我对于给出的答案并不满意,所以我想出了自己的答案。 以下是我的输入文件,并使用/ dev / urandom收集10到256个可打印字符,放入更多的目录分隔符,创建目录层次结构并在其中放置一个文件

使用urandom会创建一些非常奇怪的目录名称,这对我的目的很有帮助。 我确信一个真正的Unix大师可以更简化这个。 例如,dir的构建可能可以在一个awk命令中完成。

#!/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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐