有没有一个命令写入一个文件的随机垃圾字节?

我现在正在做一些我的应用程序再次损坏的文件的testing。 但是我发现很难findtesting文件。

所以我想知道是否有一些现有的工具,可以将随机/垃圾字节写入某种格式的文件。

基本上,我需要这个工具来:

  1. 它将随机垃圾字节写入文件。
  2. 它不需要知道文件的格式,只需写随机字节就可以了。
  3. 最好写在目标文件的随机位置。
  4. 批处理也是一个好处。

谢谢。

/dev/urandom伪设备,以及dd ,可以为你做这个:

 dd if=/dev/urandom of=newfile bs=1M count=10 

这将创build一个大小为10M的文件新文件。

如果没有足够的随机性build立, /dev/random设备经常会被阻塞, urandom不会被阻塞。 如果你使用密码级的东西的随机性,你可以摆脱随机性。 对于其他任何事情,它应该是足够的,而且可能更快。

如果你只想破坏你的文件(而不是整个文件),你可以简单地使用C风格的随机函数。 只需使用rnd()来计算偏移量和长度n ,然后使用n次来抓取随机字节来覆盖文件。


下面的Perl脚本显示了如何完成这个(不必担心编译C代码):

 use strict; use warnings; sub corrupt ($$$$) { # Get parameters, names should be self-explanatory. my $filespec = shift; my $mincount = shift; my $maxcount = shift; my $charset = shift; # Work out position and size of corruption. my @fstat = stat ($filespec); my $size = $fstat[7]; my $count = $mincount + int (rand ($maxcount + 1 - $mincount)); my $pos = 0; if ($count >= $size) { $count = $size; } else { $pos = int (rand ($size - $count)); } # Output for debugging purposes. my $last = $pos + $count - 1; print "'$filespec', $size bytes, corrupting $pos through $last\n"; 
  # Open file, seek to position, corrupt and close. open (my $fh, "+<$filespec") || die "Can't open $filespec: $!"; seek ($fh, $pos, 0); while ($count-- > 0) { my $newval = substr ($charset, int (rand (length ($charset) + 1)), 1); print $fh $newval; } close ($fh); } # Test harness. system ("echo =========="); #DEBUG system ("cp base-testfile testfile"); #DEBUG system ("cat testfile"); #DEBUG system ("echo =========="); #DEBUG corrupt ("testfile", 8, 16, "ABCDEFGHIJKLMNOPQRSTUVWXYZ "); system ("echo =========="); #DEBUG system ("cat testfile"); #DEBUG system ("echo =========="); #DEBUG 

它由你用一个文件名称调用的corrupt函数,最小和最大的损坏大小以及一个用于从中剔除的字符集组成。 底部的位是unit testing代码。 以下是一些示例输出,您可以在其中看到文件的一部分已损坏:

 ========== this is a file with nothing in it except for lowercase letters (and spaces and punctuation and newlines). that will make it easy to detect corruptions from the test program since the character range there is from uppercase a through z. i have to make it big enough so that the random stuff will work nicely, which is why i am waffling on a bit. ========== 'testfile', 344 bytes, corrupting 122 through 135 ========== this is a file with nothing in it except for lowercase letters (and spaces and punctuation and newlines). that will make iFHCGZF VJ GZDYct corruptions from the test program since the character range there is from uppercase a through z. i have to make it big enough so that the random stuff will work nicely, which is why i am waffling on a bit. ========== 

它在基本的水平上进行了testing,但是您可能会发现需要照顾的边缘错误情况。 用它来做什么。

为了完整起见,下面是另一种方法:

 shred -s 10 - > my-file 

将10个随机字节写入stdout并将其redirect到一个文件。 shred通常用于销毁(安全覆盖)数据,但也可用于创build新的随机文件。 所以如果你已经有了一个你想用随机数据填充的文件,使用这个:

 shred my-existing-file 

你可以从/dev/random读取:

 # generate a 50MB file named `random.stuff` filled with random stuff ... dd if=/dev/random of=random.stuff bs=1000000 count=50 

您也可以用人类可读的方式指定大小:

 # generate just 2MB ... dd if=/dev/random of=random.stuff bs=1M count=2