Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>/dev/urandom</code> pseudo-device, along with <code>dd</code>, can do this for you:</p> <pre><code>dd if=/dev/urandom of=newfile bs=1M count=10 </code></pre> <p>This will create a file <code>newfile</code> of size 10M.</p> <p>The <code>/dev/random</code> device will often block if there is not sufficient randomness built up, <code>urandom</code> will not block. If you're using the randomness for crypto-grade stuff, you can steer clear of <code>urandom</code>. For anything else, it should be sufficient and most likely faster.</p> <p>If you want to corrupt just bits of your file (not the whole file), you can simply use the C-style random functions. Just use <code>rnd()</code> to figure out an offset and length <code>n</code>, then use it <code>n</code> times to grab random bytes to overwrite your file with.</p> <hr> <p>The following Perl script shows how this can be done (without having to worry about compiling C code):</p> <pre><code>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 &gt;= $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"; </code></pre> <p>&nbsp;</p> <pre><code> # Open file, seek to position, corrupt and close. open (my $fh, "+&lt;$filespec") || die "Can't open $filespec: $!"; seek ($fh, $pos, 0); while ($count-- &gt; 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 </code></pre> <p>It consists of the <code>corrupt</code> function that you call with a file name, minimum and maximum corruption size and a character set to draw the corruption from. The bit at the bottom is just unit testing code. Below is some sample output where you can see that a section of the file has been corrupted:</p> <pre><code>========== 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. ========== </code></pre> <p>It's tested at a basic level but you may find there are edge error cases which need to be taken care of. Do with it what you will.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload