I want to intentionally corrupt a file on an ext4 filesystem for testing. Is there a way to corrupt it by, say, leaving a file open and then rebooting?

  • 1
    Open file, pull power plug? – Ƭᴇcʜιᴇ007 Dec 29 '15 at 21:35
  • open a file just with vim? – tarabyte Dec 29 '15 at 21:36
  • I'd rather make numerous changes to a DB and then plug out the power when the only commit to all transactions is started. – ott-- Dec 29 '15 at 22:00
  • As asked, how corrupt do you want the file? You could always open the file in question in a hex editor and remove the first couple of bytes and the last couple of bytes .. or in the hex editor, just randomly go through and make random changes to hex .. no power pulling or dd needed .. – txtechhelp Dec 30 '15 at 6:58

Do you want to be able to recover? Or how corrupt are we talking?

I would use dd, a block transfer utility.

dd if=/dev/urandom of=/dev/sdWV count=X bs=Y seek=Z


sdWV is the device you wish to corrupt

  • sdW will be the block device
  • V will be the partition number

X is the number of bocks you want to write

Y is the size of each block

Z is the place on the partition where you want to start corrupting data

Make sure you make a backup before you do any corruption.

Here, the image source will be /dev/urandom, a stream that will just generate random data, overwriting anything and everything on your device.

If you omit the count directive, it will continue writing until the device is full up.


Heh, sorry about the corruption to your current running system - your post title and body differ very much. Corrupting a filesystem and corrupting a file are pretty different...

If you want to corrupt the file, you can still use dd, but output overwriting an existing file - not the actual block device.

~ > echo "this is a test file that is about to be corrupted" > test_file
~ > cat test_file
this is a test file that is about to be corrupted
~ > dd if=/dev/urandom of=test_file bs=8 count=1 seek=1
1+0 records in
1+0 records out
8 bytes (8 B) copied, 0.007185 s, 1.1 kB/s
~ > cat test_file
this is \o♣▒▒▒_C~ >
  • 1
    Should it not be /dev/sdWV where W is the device and V is the partition number? – David Dec 29 '15 at 22:16
  • offset should be seek no? – tarabyte Dec 29 '15 at 22:50
  • i just blew away what i think was /sbin/init. is there a more controlled corruption that might just corrupt a file that lives in my home directory and force the filesystem to remount as read-only? – tarabyte Dec 29 '15 at 22:57
  • ahhh... sorry, I misread - your post title and post body differ - so you want to corrupt just a file, not the filesystem? See my edit. – Matt Clark Dec 30 '15 at 6:38
  • Can I ask, what exactly are you trying to test? This might help to better find a more adequate method of corruption. – Matt Clark Dec 30 '15 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.