Linux:创建随机目录/文件层次结构 [英] Linux: create random directory/file hierarchy

查看:122
本文介绍了Linux:创建随机目录/文件层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要测试工具,我需要一个目录,该目录包含一堆深嵌套结构的不同Office文件.我已经在目录中保存了文件,但是现在需要创建一些随机嵌套的子目录并在其中分散文件.

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 create some random nested sub directories and spread out the files in them.

我可以坐下来用自己选择的编程语言编写一个合适的程序,但是我想知道是否可能有Linux命令行工具+ Bash的巧妙组合来实现我想要的.

I could sit down and write a proper program in a programming language of my choice, but I wonder if there might be a clever combination of Linux command line tools + Bash to achieve what I want.

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

to clarify, my input is a directory with a about 200 files. The output should be a directory hierarchy containing these files more or less evenly spread. Directory names should be more than single letters, vary randomly in length and use various allowed characters (utf-8 filesystem).

推荐答案

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

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.

使用urandom会创建一些确实很奇怪的目录名,这对我来说很有用.我相信真正的Unix专家可以进一步简化这一过程.例如,可以通过单个awk命令完成dir的构建.

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

这篇关于Linux:创建随机目录/文件层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆