0

Suppose you have a file called foo.txt that is 500 bytes in size. Suppose you create a new file that is 10 bytes in size and save it as foo.txt, thus overwriting the file. Does the OS (Windows and Linux) automatically ensure that the space no longer used (410 bytes) is freed?

In a second scenario, imagine you used the dd utility to create both the old and new foo.txt as described above. Will the OS automatically ensure that the space no longer used is freed?

I imagine that behind the scenes, all writes to a file use the same OS system calls and thus handling of overwrites will be consistent across programs...

Jet Blue
  • 307

2 Answers2

2

It sounds like your question stems from viewing the process as involving several things: all unused space being available for other use, and writing a file the same name as an existing file is being written to the same location. Both of these premises are inaccurate. Also, you talk about the space no longer used being freed. The way it works, those terms are kind of synonymous; space is either allocated to a file or it isn't.

As davidgo described, drives work in whole sectors or blocks. I'll just refer to the space allocation units as "blocks" to keep it simple. Space is allocated in whole block units. A 1-byte file is allocated the entire block, so if you're discussing tiny files, anything under a block in size is still assigned an entire block. Small (sub-block) files have unused space in the block that is not accessible for another purpose. You could talk about a large file that uses multiple blocks being replaced by a smaller file that uses fewer blocks. In that case, there are whole blocks that are no longer needed.

The old file isn't really overwritten. The new file gets saved in another location, using as many blocks as it needs. The reference in the filesystem's file table to the old file's blocks gets modified. Those blocks become unassigned to any file and available for reuse. The old file's content doesn't get deleted as part of this process, it is just ignored until the space is needed. That's why you're able to recover deleted files.

You asked whether this would be different if using dd. dd can be used in a lot of ways. If you limit the discussion to simply writing a new file using the same name, that would work the same way.

fixer1234
  • 28,064
1

As @KamilMaciorowski said, a key component of the question is the block size in disk. It would be vanishingly rare to find a modern filesystem with a block size of less then 512 bytes (and most have much larger block sizes). This is relevant because it means that if you deal in file operations smaller then the block size, those operations use as much resource as the block size - so the answer to your question is NO - the 400 byte file, which takes 1 block which will be replaced with another file using 1 block. The original block will likely be freed (but might be overwritten) but you have not saved any disk space

I suspect you know this but for others - It's also worth noting that when a block is freed it is not usually overwritten/blanked out and can often be recovered. Thus is doubly true on SSDs which further abstract what us in disk from what the OS sees (thanks to wear levelling).

davidgo
  • 73,366