r/linuxadmin • • Aug 23 '19
Hard links vs Soft links
I know the difference between hard and soft links, but what I can't think of is why you would want to use a soft link over a hard link? What are some scenarios in which you would use either?
43
Upvotes
66
u/signull Aug 23 '19 edited Aug 23 '19
So 99% of the time softlinking is best when just setting things up around the command line. Writing a program however that creates files, this is usually the other way around.
Here's an example , putting a softlink `ln -s /mounts/Downloads ~/Downloads`
Here's a real scenario of me using hardlinks: I want to download a show from bittorrent and I want it to show up on my plex as soon as possible. But I want to make sure my seed ratio is 1:1 before removing it from my bit torrent client. So once i finish downloading, i hardlink it into my plex library, this is done automatically via a script i wrote that executes once a download completes. Then I also have my torrent client setup to just delete everything once the seed ratio hits 1:1. Because it's a hardlink i can delete either the original or the hardlink and as long as I still have either, the file will exist. A hardlink is just an additional pointer to a file descriptor (hence why it will only work on the same partition as the origin file).
27
u/[deleted] Aug 23 '19
You don’t always have a choice. A hard link can’t link directories, so in that case you have to use a soft link. Same with spanning file systems (you can’t make a hard link from ext3 to ext4 file system).
10
u/gordonmessmer Aug 23 '19 edited Aug 23 '19
I find that the key to understanding hard links and symlinks is that hard links a not a type of file, but symlinks are. "Hard link" is just the term we use to describe a directory entry that refers to an inode. Thus, all files are hard links[1]. Directory entries can only refer to an inode in the same filesystem, and the inode has all of the other metadata (owner, group, permissions, access/modify/change times, size, data blocks, etc). Most files have just one hard link, but POSIX filesystems allow more than one.
A symlink is fundamentally different. It's a special type of file whose content is the path to another file. The path is usually in the inode for efficiency, but if it's long enough it'll be in a data block just like any other file contents. Applications don't open this type of file the way they do a regular file, the OS handles that internally, replacing most types of file requests with the path referenced by the symlink.
1: By way of example, here is a regular file with one symlink. There is one hard link to the file and two hard links to the symlink. Note that the two symlinks have the same inode number:
7
u/HadManySons Aug 23 '19
In case you need to reference a file on a different partition?
15
u/davidsev Aug 23 '19
Properly written programs don't write files directly, instead they write a temporary file and then rename it over the original. This avoids having a brief window where the file is blank/incomplete.
With a hard link, it's not obvious it's a link; any software the writes files will thus break the link. Symlinks can be seen and treated specially.
This also applies to humans, you treat links differently to other files, with hard links you have no idea and may accidentally edit a file without realizing it.
You can't have a hard link to a directory, as every directory must have exactly one parent.
Also symlinks can point to files that don't exist, and can be relative paths. This can be handy when pointing to a file managed by software that doesn't know it needs to keep your link updated.
You also can't have hard links between different file systems.
3
u/[deleted] Aug 23 '19 edited Aug 23 '19
Usually people ask the opposite question: "why would you ever use a hard link?"
Soft/symbolic links are easier for most people to work with for everyday tasks: they are a simple and discrete reference or "shortcut" to another file or folder, that commands and shells can usually navigate gracefully. If you type
ls -lon a folder full of symlinks, it is pretty clear where those files lead to. The downside being that symlinks are not dynamic, so if you move the original file, any symlinks will break.Hard links are neat for special applications, but you wouldn't want them for everyday tasks- because they violate the typical convention of "one file, one reference" that most people take for granted.
When people
rma file, the behavior they expect is that the file in question is removed from the filesystem. When theyrma symbolic link, it is at least somewhat clear that the shortcut is a separate entity, and they are removing the shortcut and not the original. The average user doesn't know that when theyrma file, what they are really doing is unlinking a reference to an inode... and if that reference happens to be the last/only one, then the file is effectively deleted.Hard links violate this assumption about files on the filesystem being discrete things; an assumption that is true 99% of the time. The idea of a file having two equally valid reference points on a filesystem, is confusing and difficult to troubleshoot for most users.
2
u/three18ti Aug 23 '19
I have a program that automatically downloads files for me. Once it's downloaded, I want to keep the file in a common location so I can upload it to others. But, I also want to move that file to a place where it's "organized".
So I download all my files to
/opt/downloads, then I create a hardlink to/opt/foo/myfileor/opt/bar/my-other-file. I know when I'm done with it in/opt/downloadsI can just delete it because the file is still referenced from/opt/bar/my-other-file.1
u/Guirlande Aug 23 '19
A hard link is linked to a specific inode (it's a location I the filesystem referencing the physical location I the disk). A soft link is an alias to another file name, which consumes an inode. Here's the idea :
You should end up with test. If the link was symbolic, it would try to refer test_file which don't exist anymore. Then, you'd end up with "no such file or directory". To test with symbolic link, use ln -s. Also, of your were to edit or overwrite the content of the file, it would be altered in every "location".
Symbolic links = windows shortcut.
1
u/voicesinmyhand Aug 23 '19
This is sorta a TL;DR for how we got there with PAM...
Package XYZ expects file A to be in location A.
Unfortunately, file A is in location B... sometimes. Also, it can't be moved there because of other dependencies.
Package XYZ becomes so massively popular that you have to make it happy. Softlink file A in location A to location B so that it gets found.
1
u/TCM-black Aug 23 '19
Sometimes permissions. With soft links you must have permissions to all of the directories in the hierarchy to get to the linked file. With hard links, you only need permissions to that one specific inode, which is going to be the same for all hard links to that entry, but you can do things with groups and ACLs to do so.
1
u/lysergic_tryptamino Aug 23 '19
Personally, I can't think of a situation where I had to ever use a hardlink.
1
u/o11c Aug 23 '19
There are actually 3 kinds of links: soft links, hard links, and cow links.
CoW links basically invalidate the only use of hard links (reducing disk usage for identical files), but are only supported by modern filesystems. Luckily,
cp --reflink=autoexists.Symlinks are useful for files (and occasionally directories) when you want to make it visible that you're deferring to something else. One common use is
argv[0]lookup, likeunxz -> xz. Another is for things likeupdate-alternatives.Symlinks to directories are problematic because they break
..in a lot of programs, including bash completion.1
u/MartinMystikJonas Aug 24 '19
Reference different partition, relative path reference, context dependent reference (~), reference something that is available only sometimes