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…
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.