12

What is the difference between fs.link and fs.symlink?

I want to programmatically create a symbolic link to a physical file (or another symlink), and I'm on Linux, but wondered if it was possible to write a OS independant solution? What are the limitations?

Update

From the given answers and comments, Windows seems to support it.

Paul
  • 26,170
  • 12
  • 85
  • 119
Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
  • The `link` function is described here: http://www.gnu.org/software/libc/manual/html_node/Hard-Links.html#Hard-Links – Qantas 94 Heavy Nov 02 '14 at 03:30
  • this is possible if [`fs.symlink`](http://nodejs.org/api/fs.html#fs_fs_symlink_srcpath_dstpath_type_callback) wraps the creation of aliases in OS X, shortcuts in Windoze, and symlinks in GNU / Linux, which is likely. – code_monk Nov 02 '14 at 03:55
  • 2
    SO has become such a place that, even an original, not opinionated, and concise question gets downvotes without explanations. Sheessh. I don't really care the the downvotes, but at least comment :) – Yanick Rochon Nov 02 '14 at 04:13

1 Answers1

23

Linux systems have two kinds of links, hard and soft.

fs.link() is creating hard links through the C system call link(). From a terminal the equivalent is ln originalName linkName. A hard link consists of a new directory entry referencing the same file. In listings it appears to be an ordinary file, just like the original file. If the original file is removed, the content is not removed, and the hard link still works. The area of the disk is only freed when all hard links are deleted.

fs.symlink() is creating soft links, a.k.a symbolic links through the C system call symlink(). From a terminal the equivalent is ln -s originalName linkName where the -s tag denotes a soft/symbolic link. A soft link creates a special kind of directory entry that points to another file. The fact that it is a pointer is obvious when listing it, and deleting the original is sufficient to delete the content, and disrupts using the link.

I don't code on MS Windows, but this guide on symbolic links indicates that there is a mklink command for Windows command shell that can create either a hard (mklink /H) or a soft (mklink /D) link. Microsoft Developer's Network -- MSDN -- has entries for system functions CreateSymbolicLink and CreateHardLink which may provide more information about what is happening at a lower level.

On Mac, developer.apple.com's page for ln shows they have the BSD version of the ln link creation terminal command in Mac OSX 10.9, supporting both hard and soft links.

Paul
  • 26,170
  • 12
  • 85
  • 119
  • 1
    Are both these functions work on Windows? I mean, there's a mention of a `junction` type for Windows, but does a `file` or `dir` also work? I am not on Windows so I have little experience with Node.js under that environment. Do these functions just throw an error, then? – Yanick Rochon Nov 02 '14 at 03:52
  • @YanickRochon Unfortunately, I don't know. Maybe someone else does, or you can try it. I believe node will call callbacks on the async functions and provide an error. Also, I noted that the link creation commands in Windows had flags to indicate that a directory was being linked instead of a file. – Paul Nov 02 '14 at 04:04
  • yes, someone can clarify as I have no intention of installing Node.js on Windows, and I can't stand using a Mac. – Yanick Rochon Nov 02 '14 at 04:09
  • 2
    This works on Windows. – aggregate1166877 Oct 21 '22 at 14:57
  • I tested hard links on windows, but apparently, the hard links become invalid when the original file is deleted – ihsan Jul 01 '23 at 17:45