4

So I may have kind of accidentally rm -r'd ~. Not to worry, I have backups! I did a quick file listing diff to copy across files that were removed, all is good. But...

Does GNU rm corrupt files if I interrupt it? Surely it was just unlinking gigabytes of my priceless data a second, rather than removing the file from the disk itself?

2 Answers 2

5

No. rm just calls unlink() to remove the name. This syscall is atomic, meaning it either succeeds in removing the name, or does not. It can not be interrupted. If you interrupt rm while it is unlink()ing multiple files, then some files will be successfully removed, and some will not be. Housekeeping such as freeing the blocks the file was using if all links have been removed is a separate operation and may be done later, and if this is interrupted ( by a power failure or system crash, not interrupting rm ), the filesystem may require a fsck to finish it, but most filesystems these days are journaled so they do not need this. Either way, the file is either removed, or not, there is no in between.

2

From Wikipedia

To be more precise, rm removes references to objects from the filesystem, where those objects might have had multiple references (for example, a file with two different names), and the objects themselves are discarded only when all references have been removed and no programs still have open handles to the objects.

So yeah, your unlinking analogy is pretty good. If you have multiple hard links to a file, the data won't be marked ask discardable until all links are gone (symlinks don't qualify, hard links only).

If you have backups, it might be a good idea to do some md5 or sha1 hashes on the backups and your home directory, and diff them to spot any changes, but as long as you restored teh missing files, the files that stuck around shouldn't suffer any corruption.

As always, YMMV.

3
  • Hmm... To be more clear, would interrupting rm during a huge file delete cause corruption (if only half the file was deleted?) I'd find this unlikely if it only removes references, but I need to be sure.
    – Jookia
    Jan 26, 2013 at 1:39
  • If you need to be sure, then run a check using hashes. Trust but verify.
    – peelman
    Jan 26, 2013 at 1:48
  • 1
    Eh, I don't think I have a slow enough computer to test the hypothesis. I'll go read the source code.
    – Jookia
    Jan 26, 2013 at 1:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .