Are hard links equivalent to Windows shortcuts?

10,892

Solution 1

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:

enter image description here

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?

enter image description here

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.

Solution 2

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?

Solution 3

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'.

Solution 4

On Windows you can create hardlinks too if you have NTFS filesystem.

fsutil hardlink create target_file source_file

The files has to be on the same logical drive.

Solution 5

A big difference , hard-link cannot be created for folders , but for files .

Shortcut can be created for folders , so you cannot say they are equivalent .

Your question should be the difference between symbolic link or soft link and shortcut .

as according to this:

A symbolic link is filesystem level, and everything sees it as the original file. An application needs no special support to use a symbolic link.

A "Shortcut" is just a regular file that has a reference to the destination file or directory .

So when you click a shortcut will change your directory to the actual file , while soft-link will refer to its location as if its the actual file , for that in Linux you can use terminal and cd to symbolic links while you cannot cd to shortcuts .

A Windows shortcut and a Linux launcher (pointing to some location) would be identical.

Share:
10,892

Related videos on Youtube

Computernerd
Author by

Computernerd

I like to ask questions

Updated on September 18, 2022

Comments

  • Computernerd
    Computernerd almost 2 years

    Wikipedia defines a hard link as:

    a directory entry that associates a name with a file on a file system. (A directory is itself a special kind of file that contains a list of such entries.) The term is used in file systems which allow multiple hard links to be created for the same file.

    I am wondering if the concept of hard link is equivalent to the Windows concept of Shortcut.

    If hard links are not equivalent to shortcuts, then what's the closest Windows feature to hard links?

  • terdon
    terdon over 10 years
    What? Softlinks are just files, albeit strange ones, they also take up inodes for example. They are NOT treated as the original (that's hardlinks). While it's true that hardlinks are not allowed for directories in most OSs, this is just a safety feature and there are exceptions to this (OSX time machine for example). And of course you can cd to shortcuts (I assume you mean Windows shortcuts since as you say you can cd to soflinks).
  • nux
    nux over 10 years
    are you sure you can use cmd to cd to a shortcut .
  • terdon
    terdon over 10 years
    I just fired up my Windows VM and created a shortcut to a folder on my Desktop, windows shortcuts are very similar (if not identical) to symlinks. While you cannot cd to them using cmd.exe for some reason, you can create a .lnk to a directory and use it via explorer.
  • Kiwy
    Kiwy over 10 years
    @terdon sysmlink does exist anyway on recent NTFS
  • nux
    nux over 10 years
    @terdon see the differance superuser.com/questions/253935/…
  • nux
    nux over 10 years
  • nux
    nux over 10 years
  • nux
    nux over 10 years
    down voters should give a reason for downvoting
  • terdon
    terdon over 10 years
    Sorry, still not correct. Symlinks are files containing their target as plain text (see here), symlinks are not treated as the original, that's the whole difference between symlinks and hardlinks. Linux launchers (I assume you mean .desktop files) have nothing to do with this at all, they are completely different. As you yourself say, Windows shortcuts can point to files (including programs) and folders (mac and windows have folders, Linux has directories) .desktop files just allow you to launch a program. They don't link anywhere.
  • Emmet
    Emmet over 10 years
    It's also worth noting that deleting (via rm or unlink()) a hard link only decrements the link count until the link count reaches zero, when the blocks occupied by the file contents are released. I'm not sure that saying “hard links are actually two instances of the same file” is strictly correct, since the two names point to the same inode, and thus the same content, rather than there being two instances of the file itself in any sense.
  • Joey
    Joey over 10 years
    Note that a Windows shortcut is a file used by the shell and thus can do things that the shell knows about, but not the file system. E.g. you can make shortcuts into the shell namespace (e.g. Control Panel, Computer, etc.), or you can make shortcuts to applications yet to be installed (which will be installed at first use). Those things have no direct equivalent in the file system world.
  • mikebabcock
    mikebabcock over 10 years
    I love this answer but its actually quite poor at understanding what a Windows shortcut is. @Shaakunthala below has a better response as the Windows shortcut is more like a Linux .desktop file than a symlink.
  • cHao
    cHao over 10 years
    NTFS actually works very much like this. A file is an entry in the MFT, and directory entries simply map filenames to file IDs. It supports hardlinks and, since 2008/7/Vista(?), even symlinks. At this point, the biggest difference is cultural.
  • Tobias
    Tobias over 10 years
    That's correct - NTFS supports hardlinks, too. Often the tools require you to have admin privilegues to create them. About the "same logical drive" - it would be more precise to say, they need to be on the same filesystem. A logical drive could be anything, including a drive letter created by the subst command. A tool might not know/care for this and thus refuse to create a hardlink, though. A very useful thing is the Link Shell Extension which enables the explorer to create them.
  • KcFnMi
    KcFnMi almost 4 years
    Does this answer says if I create a hard link and edit the original file, then the changes can be seen on the link? I tried that on macOS and it seems the files (original and link) are unrelated.
  • terdon
    terdon almost 4 years
    @KcFnMi yes, "hard links" are the same file. They are two entries in the file system pointing to the same file. "soft links" are closer to what you think of as links. However, in both cases, any changes made in one will be seen in the other. If you don't see this, you probably used an editor which made a copy of the file. Try adding something to the file from the command line. If hardlink is the hard link, do echo "foo" >> hardlink. Then open the original file and you will see foo has been added.
  • KcFnMi
    KcFnMi almost 4 years
    Looks like you are right and xcode is playing with me.
  • FlexMcMurphy
    FlexMcMurphy over 3 years
    Thumbs up for this question because it actually explained that A "Shortcut" is just a regular file.