A mental model for Linux file, hard and soft links
bhoot.dev
bhoot.dev
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.
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.
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!
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.
-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.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.
On a copy-on-write filesystem, linking may not have much utility vs. copying.
- 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?
[~] 0 $ mkdir tmp/demo
[~] 0 $ cd tmp/demo
[demo] 0 $ ln -s foo bar
[demo] 0 $ ls -l
total 1
lrwxrwxrwx 1 user users 3 Nov 15 12:14 bar -> foo
[demo] 0 $ cat bar
cat: bar: No such file or directory
[demo] 1 $ echo foo > foo
[demo] 0 $ ls -l
total 2
lrwxrwxrwx 1 user users 3 Nov 15 12:14 bar -> foo
-rw-r--r-- 1 user users 4 Nov 15 12:14 foo
[demo] 0 $ cat bar
foo
[demo] 0 $ rm foo
[demo] 0 $ cat bar
cat: bar: No such file or directory
[demo] 1 $ ls -l
total 1
lrwxrwxrwx 1 user users 3 Nov 15 12:14 bar -> foo
[demo] 0 $
What you can't see because this is flat text is that in my terminal the first and last "bar -> foo" are red because ls is warning me that that link points to a file that doesn't exist.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.
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_resolutionMy 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.