When would you use one over the other?
The different semantics between hard and soft links make them suitable for different things.
Symbolic links (soft links)
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.
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
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.
Hard links are just references to the same disk spaces, thath the 'why' you cannot hardlink something in other filesystem.
Symlinks are files linking other files (as Windows shortcuts), maybe in the same filesystem, maybe not.
EDIT: I will explain something more. Every file that exists has a minimum of 1 hard link. Hard links are the way to access the content of an inode of the filesystem. You can obtain the inode number of a file with
Thanks @geekosaur for this reference:
and this (edited):
"hard" links share the same inode
If I edit either foo or foolink there is only one file and it will be updated. If I remove only one of the filenames, the inode and data will persist, foolink will survive.
If I were to create the same, but with a "soft" or symbolic link, then There's one file, one inode, and a new file with its own inode pointing to the first.
If I edit either foo or foolink there is still only one file and it will be updated.
If I remove only the symlink, the inode and data will persist. If I remove foo, the data will be gone, the symlink will persist but point to a non-existent file.
A soft link points to another pathname. That pathname may or may not actually exist. The path isn't looked for until you access the symlink. If the path doesn't exist when you try to access it, you have a broken symlink.
With a hard link, you have one file with multiple names. You can't say that one of those is the "real" file and the others are just a link to it. They are all equal. There's no such thing as a broken hard link the way there are broken symlinks.
Hard links work only within a single filesystem. If you want to link to a file on a different filesystem (e.g. a different partition or a network share), you must use a soft link.
Another big difference is what happens when you delete a linked file. If you delete one of a pair of hardlinked files, then create a new file with the same name, you'll have two separate files (the link is gone). If you delete the target of a symlink and create a new file with the same name, the link will point to the new file.
A hard link will keep a file on disk until all hard links to it, even the first (a "filename" is technically a hard link), have been deleted. A soft link can be left "dangling" until the file it point(s/ed) to is replaced.
HARD LINK (Only Files) vs SOFT LINK (Files or Directories) vs BIND (HARD LINK for Directories)
While daxelrod's answer explains the question well, I thought that the picture in this case made a big difference, especially to beginners who don't understand inodes and complicated Linux jargon quite yet.
Think of this, if you "deleted" everything from your drive, you could run software to restore the data, because the 1's and 0's are still there, you just deleted all the Hard Links. Recovery Software's purpose is to rebuild the Hard Links to make sense of the 0's and 1's
I read a great "one liner" that made this all make sense and I wanted to share!
All files in Linux are "Hard Links" to the 0's and 1's on the disk. When you create a data (0's & 1's) the OS creates a Hard Link in the File Tree to reference that spot on the hard disk.
Create HARD LINK 2 and delete HARD LINK 1 Original File:
You may create another hard link and delete the original file, and you still have access to the newly created hard link.
Delete FILE(HARD LINK 1) that is SOFT LINKed to:
If you deleted the HARD LINK 1, do you think the SOFT LINK would work? No, The OS will report back that the HARD LINK 1 does not exist.
Delete SOFT LINK to HARD LINK:
In reverse, if you delete the SOFT LINK, will either HARD LINK work? Yes. As long as the OS has one HARD LINK File it will report that the fill has not been deleted.
BIND -- BIND is a HARD LINK for Directories, You CAN NOT HARD LINK DIRECTORIES.
While soft and hard links use
and can be configured via etc/fstab.
Command line example of binding HARD LINK 1 and HARD LINK 2
mount -B "HARD LINK 1" "HARD LINK 2"
mount -B SourceDirectory DestinationDirectory
mount -B /home/uploads /ftp/uploads
Now any changes to /home/uploads are reflected in /ftp/uploads
Delete HARD LINK 2 BIND
What Happens in Linux if you try to mount something and the mount point does not exist? It fails. So basically you will get a mount error, but HARD LINK 1 will be fine.
Delete HARD LINK 1 (Source Dir) BINDed to HARD LINK 2 (Mount Point/Destination Dir).
In this case, you have deleted the source directory and the destination directory will fail to mount. Like trying to mount a drive that doesn't exist.
Bind is when you "Bind" two directories. What it means is any change to the Original HARD LINK (Source) Will show in the Mount Point/Destination of the Bind.