Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between fs.link and fs.symlink? Are they platform independent?

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.

like image 443
Yanick Rochon Avatar asked Nov 02 '14 03:11

Yanick Rochon


People also ask

What is FS link?

The fs. link() method is used to create a hard link to the given path. The hard link created would still point to the same file even if the file is renamed.

What is the symbolic link in Nodejs?

In the nodejs file system module there is a method for creating symbolic links to files and folders. A symbolic link is just simply a reference to a folder or file rather than a folder of file itself.

What is a symlink Javascript?

A symbolic link(or symlink) is used to describe any file that contains a reference to some other file or directory which can be in the form of a relative or absolute path. In a way, you can say a symlink is a shortcut file.

What is FS in Nodejs?

Node.js as a File Server var fs = require('fs'); Common use for the File System module: Read files. Create files. Update files.


1 Answers

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.

like image 60
Paul Avatar answered Sep 28 '22 00:09

Paul