Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a symlink creation by overriding the existing symlink?

Tags:

I use the fs module to create symlinks.

fs.symlink("target", "path/to/symlink", function (e) {    if (e) { ... } }); 

If the path/to/symlink already exists, an error is sent in the callback.

How can I force symlink creation and override the existing symlink?

Is there another alternative than check error + delete existing symlink + try again?

like image 881
Ionică Bizău Avatar asked Apr 23 '15 09:04

Ionică Bizău


People also ask

Can you symlink to another symlink?

In general, no. Technically, there will be a very slight performance hit for the indirection, but it won't be noticeable to your application. As an example, most shared libraries are symlinks to symlinks (e.g. libQtCore.so -> libQtCore. so.

What happens when you delete the source to a symlink?

If a symbolic link is deleted, its target remains unaffected. If a symbolic link points to a target, and sometime later that target is moved, renamed or deleted, the symbolic link is not automatically updated or deleted, but continues to exist and still points to the old target, now a non-existing location or file.

Does removing symlink remove file?

When you remove a symlink, the file it points to is not affected. Use the ls -l command to check whether a given file is a symbolic link, and to find the file or directory that symbolic link point to. The first character “l”, indicates that the file is a symlink. The “->” symbol shows the file the symlink points to.

Can symlinks be copied?

We can use the -l option of rsync for copying symlinks. rsync copies the symlinks in the source directory as symlinks to the destination directory using this option. Copying the symlinks is successful in this case.


1 Answers

When using the ln command line tool we can do this using the -f (force) flag

ln -sf target symlink-name 

However, this is not possible using the fs API unless we implement this feature in a module.

I created lnf - a module to override existing symlinks.

// Dependencies var Lnf = require("lnf");  // Create the symlink Lnf.sync("foo", __dirname + "/baz");  // Override it Lnf("bar", __dirname + "/baz", function (err) {     console.log(err || "Overriden the baz symlink."); }); 

Read the full documentation on the GitHub repository

like image 197
Ionică Bizău Avatar answered Sep 28 '22 01:09

Ionică Bizău