Linux使用随机或伪随机数据递归填充所有文件

时间:2016-03-07 01:35:14

标签: linux random byte fill dd

我想从根目录中填写随机字节ALL文件RECURSIVELLY。只有文件,文件夹将保持未开封

我不想删除文件,只需用随机字节填充它们

在我的研究中,单个文件的填充命令可能类似于:

dd if=/dev/random of=target-files bs=1M

要以递归方式查找所有文件,我应该使用:

find . -name "* .*"

我的问题是:

  1. 通过加入这两个命令可以实现我的目标吗? (管他们?怎么样?)

  2. 还有另一种更简单的方法可以达到相同的效果吗?

  3. 感谢您的帮助; - )

4 个答案:

答案 0 :(得分:1)

您可以尝试将查找结果传递给while循环,并将dd / dev / udrandom传递到每个文件中。

$ find myfolder -type f |while read fd; do dd if=/dev/urandom of=$fd bs=1M count=1;done

如果你想保留文件大小,可以在找到1k块计数时进行一些计算,并在dd中作为args传递。

$ find myfolder -type f|while read fd; do FSIZ=`stat -c %s $fd`; CNT=`expr $FSIZ / 1024`;dd if=/dev/urandom of=$fd bs=1k count=$CNT;done 

答案 1 :(得分:0)

查找+ xargs + dd + wc会这样做。

 find / -type f -print0 | xargs -0 -Ifilename  sh -c 'dd if=/dev/urandom of="filename" bs=`wc -c "filename" | cut -d" " -f1` count=1'

工作原理:

  1. find + xargs迭代从/
  2. 开始的所有文件
  3. -print0 in find + -0 in xargs处理边缘情况,如空格/引号/连字符等文件名等
  4. " wc -c"提供以字节为单位的文件大小,cut从输出中提取第一个字,其大小以字节为单位
  5. dd用wc + cut
  6. 提供的大小填充输出文件
  7. -I in xargs指定文件名的变量

答案 2 :(得分:0)

第三种方法:)

find / -type f -printf "%s\t%p\n" | while read size name ; do dd if=/dev/urandom of="${name}" bs=1c count=${size}; done

准备等待很长时间:}

答案 3 :(得分:0)

我爱 dd (真的:我喜欢它!);但是,在这种情况下,你可以使用 shred ,专为实现这样的目的而设计。 它是 GNU coreutils 包的一部分(安装在[几乎]所有Linux系统上)。

  

find rootdirectory -type f -print0 | xargs --null --max-args=1 shred --force --iterations=4 --random-source=/dev/urandom --verbose

相关问题