Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

If you do rm myFile where myFile is a hard link, what happens?

share|improve this question
add comment

2 Answers 2

up vote 34 down vote accepted

In Unix all normal files are Hardlinks. Hardlinks in a Unix (and most (all?) ) filesystems are references to an to what's called an inode. The inode has a reference counter, when you have one "link" to the file (which is the normal modus operandi) the counter is 1. When you create a second, third, fourth, etc link, the counter is incremented (increased ) each time by one. When you delete (rm) a link the counter is decremented ( reduced ) by one. If the link counter reaches 0 the filesystem removes the inode and marks the space as available for use.

In short, as long as you do not delete the last link the file will remain.

Edit: The file will remain even if the last link is removed. This is one of the ways to ensure security of data contained in a file is not accessible to any other process. Removing the data from the filesystem completely is done only if the data has 0 links to it as given in its metadata and is not being used by any process.

This IMHO is by far the easiest way to understand hard-links (and its difference from softlinks).

share|improve this answer
2  
Moreover, the system call for deleting a file is unlink(). –  mouviciel Oct 8 '12 at 5:38
1  
This doesn't cover the situation where the file is open when the last link is unlinked. –  cjm Oct 8 '12 at 6:26
    
@cjm Opening a file adds a new hardlink in /proc. The same logic then appiles. –  OrangeDog Oct 8 '12 at 8:48
    
@OrangeDog, not exactly, because hardlinks can't cross filesystems, and /proc is a separate (virtual) filesystem. –  cjm Oct 8 '12 at 9:18
    
/proc also mirrors the kernel's internal data structures (it's a way for the Linux kernel to expose certain data in a reasonably well-defined format without giving all and sundry direct access to kernel memory). So it's more accurate to say that the kernel keeps track of the fact that the file is open, and exposes that information through procfs. –  Michael Kjörling Oct 8 '12 at 11:11
show 1 more comment

Testing was easier than I thought: I created a text file, then hard linked to it. Deleting the hard link does not delete the file it is hardlinked to and the file that was linked to remains where it is.

share|improve this answer
1  
this is true, but not a complete picture –  xenoterracide Oct 8 '12 at 5:26
    
The key is that creating the text file also adds a hard link. In *NIX filesystems, all files (inodes) must be hardlinked at least once into the directory structure. –  OrangeDog Oct 8 '12 at 9:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.