Are hard links equivalent to Windows shortcuts?
No, a hard link is completely different. A soft link is closer to a Windows shortcut (though there are important differences, symbolic links are more similar to windows shortcuts than hard links are). A hard link is a different thing and one you will almost never need.
Briefly, a soft link is created with this command:
ln -s foo bar
If you then run ls -l
, you will see:
lrwxrwxrwx 1 terdon terdon 3 Mar 10 15:58 bar -> foo
-rw-r--r-- 2 terdon terdon 0 Mar 10 15:58 foo
The ->
means that bar
is a link to foo
. So, opening bar
, with a text editor for example, will actually open the separate file foo
. However, deleting bar
will just delete the shortcut, it will not affect the file foo
.
Hard links, on the other hand, are created with this command:
ln foo bar
If you now run ls -l
, there is no indication of any relationship between the files:
-rw-r--r-- 2 terdon terdon 0 Mar 10 15:58 bar
-rw-r--r-- 2 terdon terdon 0 Mar 10 15:58 foo
But—and this is very important—those are actually the same file. Files on Unix file systems are stored using inodes; an inode is basically the way the filesystem maps a file name to a particular location on the physical hard drive. So, hard links are files that point to the same inode as their target. Another way of putting this is that all files are actually hard links pointing to their inodes. Making a hard link to a file just creates a new pointer (file) on the file system that points to the same inode. Each inode can have multiple files pointing to it or one, or none.
To understand this more clearly, use ls -i
which shows the inode associated with a file. Let's create a soft link and a hard link and see what happens:
ln -s foo SoftLinkToFoo
ln foo HardLinkToFoo
Now, check their inodes:
As you can see above, both foo
and HardLinkToFoo
have the same inode (16648029) while SoftLinkToFoo has a different one (16648036).
What happens if we rename foo
with mv foo bar
?
The red color indicates a broken soft link, one whose target can no longer be found. This is because soft links point to a file's name, not its inode. Note that despite changing the name, the inode remains the same so the hardlink is fine, it still works.
In summary, hard links are actually two manifestations of the same file; they are pointers to the same section of the disk. Soft links are just shortcuts. To take a real world analogy, hardlinks are like two different phone numbers for the same phone line and soft links are like having two different phone lines in the same house.
There's a good explanation of what soft and hard links are, but one thing needs to be clarified.
Windows shortcuts are equivalent or similar to neither soft links nor hard links. On the file system level they are just files. It's the shell that understands their structure and interprets them as links. Windows shortcuts can also point to objects in shell namespaces which aren't related to the file system (printers, control panel items, virtual folders).
Windows shortcuts, in addition to the name of the file system object, contain the following information: PIDL (opaque binary "path" within shell namespace), description, hotkey, icon, working directory. Windows also adds NTFS object identifiers if NTFS file system is used, to fix broken shortcuts.
The rough equivalent of a Windows shortcut is a .desktop file. See this question on SuperUser: Is there an equivalent of .lnk in Linux?
No. In Linux things work differently.
Each file is represented by an object called 'inode'. Every inode has a number (ID) associated with it.
As we know humans are not good at remembering numbers but names. (That's how phonebooks evolved)
Therefore, filename came into the picture to give each inode a human readable name. Basically, a hardlink binds a filename to an inode. An inode can have multiple hardlinks. If there are no hardlinks present for a particular inode, disk space used by the inode may be re-allocated for new files. Which means, at least one hardlink must present for each file. The filename (visualized as the filename/icon you see in file browser) itself is a hardlink.
In Windows, shortcut is a separate file (*.lnk file). It contains information about the original file (understandably the file path). In Linux perception, a Windows shortcut would be another inode hardlinked to a filename ending with '.lnk'.