Say I have a Linux system and want to write to a raw device. But I want to replace a certain file on this device with a new version. Can this be accomplished? I think with dd you could maybe write the new file starting from the offset of the old version. I guess if the new file has the same or smaller size as the old one everything should be fine. But what would happen if the new file is larger but the disk itself is not full? Would it overwrite other files and corrupt the filesystem? If so, can you prevent that? Can you replace files on compressed filesystems like btrfs too?

  • What (as in software, or driver etc) do you expect will confine your writes to the clusters/sectors occupied by your file? Short of implementing your own file system, it's very difficult. You are not saying what you are doing, so this is a pretty pointless discussion.l
    – ajeh
    Aug 11, 2018 at 17:45

1 Answer 1


You need to mount the filesystem. There is no such thing as “the offset of the old version”: files can be fragmented. In order to modify the data, you would need to find the offsets and sizes of all the fragments. It would be even worse if the new version had a different size: you'd need to figure out where there is some free space for the extra data, and you'd need to update the directory data structures to indicate where the new blocks are.

You may find some code to do this in filesystem debuggers, but they are hard to use reliably and usually don't support writing much. The only easy-to-use, reliable code to modify data on a filesystem is the filesystem driver, which you access by mounting the filesystem.

A few filesystems (e.g. an optional feature of zfs and btrfs) even have file integrity data — they keep checksums of files or blocks to detect data corruption. With such filesystems, if you change the content of a data block in a file without changing the corresponding integrity data, you won't be able to read it, since the file will be corrupted.

There are a few cases where it may be possible to bypass the driver to overwrite a file's data without changing the file size, for example with some filesystems designed to be read-only (i.e. you prepare the filesystem, and then you write it to some flash memory and don't update it). If the data is compressed, it's only viable if the compressed size remains the same, of course. But generally speaking, mounting is the only way.

  • I think with "mounting is the only way" you mean changing files on the the mounted filesystem? Or is there some way to use the information gathered by mounting the device and then writing on the raw device in a reliable way. A particular filesystem I had in mind when asking the question was the iso filesystem for cd/dvd images. This is read only by software. But when burned to an USB stick you can still write to the raw device. Do you see any chance to write to the iso image in a reliable way in this specific case?
    – hid01
    Aug 11, 2018 at 14:59
  • @hid01 The ISO filesystem is a read-only-only filesystem, so it may be one of those places where it's possible as long as the file size remains the same, as I mention in my last paragraph. I didn't list it explicitly because I'm not familiar with it, so I don't know whether it would actually work. Aug 11, 2018 at 15:08
  • Would it really need to be exactly the same size or could it be smaller too? In case the file would be larger would it destroy other files in each case or would it be like "russian roulette" ?
    – hid01
    Aug 11, 2018 at 15:12
  • 1
    If you don't update the directory information and only overwrite the beginning of a file, the end will still be there. If you write more than the length of the file, you'll only see as much data as there was before, and it's Russian roulette regarding what you'd destroy. Aug 11, 2018 at 15:14
  • There is WinIso for manipulating ISO images. I am at a loss to understand why homegrowing something could be a better option.
    – ajeh
    Aug 11, 2018 at 18:42

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.