What is the difference between symbolic and hard links?
The different semantics between hard and soft links make them suitable for different things.
Hard links:
- indistinguishable from other directory entries, because every directory entry is hard link
- "original" can be moved or deleted without breaking other hard links to the same inode
- only possible within the same filesystem
- permissions must be the same as those on the "original" (permissions are stored in the inode, not the directory entry)
- can only be made to files, not directories
Symbolic links (soft links)
- simply records that point to another file path. (
ls -l
will show what path a symlink points to) - will break if original is moved or deleted. (In some cases it is actually desirable for a link to point to whatever file currently occupies a particular location)
- can point to a file in a different filesystem
- can point to a directory
- on some file system formats, it is possible for the symlink to have different permissions than the file it points to (this is uncommon)
The point of both types of links is to provide a way to make a file appear in two locations at the same time. This has a lot of uses. 9 times out of 10 you want to use symbolic links.
Symbolic links, or "symlinks" work a little like Windows shortcuts. The contents of a symlink are a pointer to the real location of the file/directory. If you delete the real file, the symlink will become "dangling," and won't work. Deleting the symlink does not delete the real file. You can have as many symlinks to a single file (or even other symlinks) as you like.
Unlike Windows though, they work on the filesystem level, not shell or application level, so pretty much any application will "follow" symlinks as expected. ls -al
can be used as a quick way to see where symlinks "point" to.
Hardlinks work even on a lower level. A hardlink is an actual, physical on-the-filesystem-level directory entry of the file. Technically, a directory entry is a hardlink, thus each file has at least one hardlink in a directory somewhere. Hardlinks are not separate from the file they point to; if a file has multiple hardlinks in different directories, deleting the hardlink with utilities like rm
won't truly delete the file, until all hardlinks are gone.
I can't think of situation where use of hardlinks is common, or even needed, unless you intentionally want to prevent the files from getting deleted or are doing some weird low-level work with partitions or other filesystem related things. EDIT: There's great ideas in the other answers to this question, though!
Hard links are very useful for disk-based backup mechanisms, because you can have a full directory tree for each backup while sharing the space for files that haven't changed — and the filesystem keeps track of reference counting, so when the last reference to a given version goes away because the backup was expired/removed for space reasons, the space it used is automatically reclaimed. Some mail clients also use it for messages filed to multiple folders, for the same reason.