4 ms·
A mental model for Linux file, hard and soft links
- blueflow 2y agoYou can also type "man 7 inode" and "man 7 symlink" into your terminal and have the information from the primary source.
- froh 2y agoand wait 'til the author learns about `cp --archive`, copying the symlink instead of the file contents... no I shouldn't be so snarky. it's good that people share their learning experiences. I apologize. original author, you have a section on "cp", where you note plain "cp" copies the contents of the linked-to file. there also is an option "cp --archive" which copies the symlink as such.
- _benj 2y agoWow, this was super helpful! I’ve always have that underlying question of “why is it always ln -s” and ln -s just became a muscle memory without any reason. Good post!
- jas39 2y agoI would recommend relative symlinks (ln -rs). Makes it easier to rename the homedir for example. On a copy-on-write filesystem, linking may not have much utility vs. copying.
- blueflow 2y agoOpen a terminal, type "man 1 ln". When the text shows up, you are usually in a less(1) paged window - type "/-s" to search for the option, "N" key to search forward, shift+"N" to search backwards. If you see a reference like $name($number), you can get to the manpage with "man $number $name". "man man" for the general man invocation, and "man less" on how to interact with the less pager.
- billpg 2y agoYou could, but this website's better.
- blueflow 2y agoWhy rely on guesswork when you have the primary source at hand?
- xboxnolifes 2y agoBecause the primary source's explanation is: -s, --symbolic make symbolic links instead of hard links While a fine reference, it doesn't exactly explain what that means if you don't already understand links.
- blueflow 2y agoman 7 symlink GNU's ln(1) unfortunately does not link to symlink(7) directly, but indirectly via symlink(2). You cal still find it with the "apropos" command.
- rirze 2y agoSame, after learning the hard (pun intended?) way about hark links, I just default to soft links.
- athoscouto 2y agoNice! Two questions for me to follow up later: - How the OS knows it can clean up an inode after a hard link is deleted? The post mentioned inodes don't see hard links - What does it mean to have a dead/dangling soft link?
- echoangle 2y agoFor 1, the inode probably has a reference count that's incremented when creating a hard link and decremented when deleting one. If the count is 0, the inode can be deleted.
- FranchuFranchu 2y agoThis is correct, for ext2 at least. See [0], bytes 26 and 27 [0] https://wiki.osdev.org/Ext2#Inode_Data_Structure https://wiki.osdev.org/Ext2#Inode_Data_Structure
- kreetx 2y agoI know its required to store this count such that the filesystem would know when it can actually delete the inode, but isn't this half-way to making the inode aware of the paths pointing to it?
- actionfromafar 2y agoAFAIK between two hard links, both are equal. None is more "the real" file than the other.
- jagged-chisel 2y agoYou can point at softlink at any path, even one that doesn’t exist. Create a regular file, now softlink to it, delete the regular file - now your softlink is dead.
- dspillett 2y ago1. This depends on the filesystem. For ext2/3/4 (and many others) there is a reference count maintained in the first inode of the file. You can usually see this count in the output of "ls -l", between the perms and ownership columns. If someone goes wrong and the count isn't decremented properly (due to a system crash while the inode is being updated) or is otherwise corrupt, the space allocated to the object may never be released when it is deleted because the count will never reach zero. This is one of the checks/fixes fsck.ext* does when run. If the count is somehow too low the content could be deallocated too early, resulting in corruption (the remaining link(s) ending up pointing to the wrong data when the inode is eventually refused). Again fsck can detect this, but only if it is not too late and things are already mislinked or some of the space relocated. 2. A dangling soft link points to nothing valid. If you try to access it in a way that would normally give you the object it points to there will be a not found error. If a new object of the destination name appears the link will start to work again but give the new content. If relative links are moved around out of step with what they point to this can cause significant confusion. This is not filesystem level corruption that fsck can/will check for.
- libertarian1 2y agoThe pics are VERY difficult to read
- jbhoot 2y agoSorry about that! I hand-wrote the whole post on my tablet. More motivation for me to write more legibly next time!
- froh 2y agoHi bhoot, you have a section on "cp", where you note plain "cp" copies the contents of the linked-to file. there also is an option "cp --archive" which copies the symlink as such. also someone else pointed out you may want to add man 7 inode man 7 symlink man 7 path_resolution
- jbhoot 2y agoThere is also "cp -P". My purpose for the I/O sub-section was only to have a convenient logic of memorising how I/O operations work on soft links by default. Memorising techniques don't really gun for technical accuracy. However, I have struck it out because the logic is also blatantly inaccurate and has been rightly called out here.
- deleted 2y ago[deleted]
- pjmlp 2y agoIn UNIX actually.
- jbhoot 2y agoOP here. Thank you for posting this here, and for all the corrections. I have updated the post with them. My idea behind a mental model is to describe how I visualise a concept in my mind when I think about it. The level of abstraction of a mental model is supposed to keep out the details of implementation, ensuring which I failed at. Because the deeper you dig from this level, the more inaccurate things will turn out to be. A mental model for me is meant to be only a starting point for a concept. In other words, the aim is such that when you think about a concept, its mental model should put you into the right perspective. Right, this is how something works, now let's check out the manual to see how I can put it to use. I've now configured my site to add this explainer to a post classified under Mental model.
- pixelbeat 2y agoYou might find my "symlinks, hardlinks and reflinks explained" article useful: https://www.pixelbeat.org/docs/unix_links.html https://www.pixelbeat.org/docs/unix_links.html