Search code examples
linuxunixfilesystemsposix

How does rm work? What does rm do?


My understanding is that 'files' are effectively just pointers to the memory location corresponding to the files content. If you 'rm' a file, you certainly must be deleting that pointer. If rm actually 'erases' the data, I would guess each bit is written over (set to 0 or something). But I know that there are special procedures/programs (i.e. srm) to make sure that data isn't 'recoverable' --- which suggests that nothing is actually being overwritten...

So, is deleting the pointer to a memory address the only thing rm does? Is the data still sitting there in a contiguous block like it was before?


Solution

  • My understanding is that 'files' are effectively just pointers to the memory location corresponding to the files content.

    Be careful with your terminology. The files (and pointers) are on disk, not in memory (RAM).

    If you 'rm' a file, you certainly must be deleting that pointer.

    Yes. What happens is heavily file-system dependent. Some have a bitmap of which block are free/busy. So it would have to flip the bit for each block freed. Other filesystems use more sophisticated methods of tracking free space.

    which suggests that nothing is actually being overwritten...

    Correct. You can find various "undelete" utilities. But depending on the filesystem, it can get rather complex. But stuff you saved years ago could still be sitting around -- or it could be overwritten. It all depends on minute details. For example, see e2fsprogs.

    So, is deleting the pointer to a memory address the only thing rm does?

    Well, it also has to remove the "directory entry" that gives metadata about the file. (Sometimes it just wipes out the first byte of the filename).

    Is the data still sitting there in a contiguous block like it was before?

    Yes, the data is still there. But don't assume it is a contiguous block. Files can be freagmented all over the disk, with lots of pointers that tell it how to re-assemble. And if you are using RAID, things get real complex.