1

I have several JavaScript projects that use the same set of utility scripts.

Today after copying (cp) the library folder for the dozenth time, I did some research I found the ln && unlink commands.

I used ln to create a symbolic link to the lib folder, like this:

ln -s -t ~/Development/Web/Project.js ~/Development/Libraries/Main.js/lib

From what I understand, the previous command:

  1. creates a symbolic link using -s
  2. creates a link to the lib folder from ~/Development/Libraries/Main.js in the ~/Development/Web/Project.js folder
  3. when I deleting the link using unlink ~/Development/Web/Project.js/lib it doesn't affect (modify) the lib folder in the Main.js folder
  4. a symbolic link is a (non mutable) pointer to the the target inode
  5. a hard link which creates a new with a link to the target's inode
  6. you can also link/unlink files using the same commands

Are my assumptions correct?

2

Most of your assumptions appears correct to me, only the 4th point is incorrect. A symbolic link is a path to the target, not a pointer to the inode of the target, that's why symbolink links work across file systems.

To demonstrate how the unlink command works I created a few files and a directory in which I will create the links:

~$ touch test
~$ touch test1
~$ mkdir dir

Now I create a symbolic link to test and a hard link to test1

~$ ln -s -t dir test
~$ ln -t dir test1
~$ ls -l dir
total 0
lrwxrwxrwx 1 user user 4 Jul 23 19:21 test -> test
-rw-rw-r-- 2 user user 0 Jul 23 19:22 test1
~$ ls -l test1
-rw-rw-r-- 2 user user 0 Jul 23 19:20 test1

You can see that the hard link dir/test1 has a hardlink-count of 2, also the target test1 has a has a hardlink-count of 2.

Now I remove the symbolic link dir/test

~$ unlink dir/test
~$ ls -l dir
total 0
-rw-rw-r-- 2 user user 0 Jul 23 19:22 test1
~$ ls -l test
-rw-rw-r-- 1 user user 0 Jul 23 19:20 test

We see that the link is removed, but the original file test still exists.

Now I remove the hard link dir/test1:

~$ unlink dir/test1
~$ ls -l test1
~$ ls -l dir
 total 0
~$ ls -l test1
-rw-rw-r-- 1 user user 0 Jul 23 19:20 test1

And again we see that the link is removed, but the original file test1 still exists, but its hardlink-count is now 1.

I never used the unlink-command, you will achieve the same result with the rm-command.

  • Thanks for taking the time and putting in the effort to explain this to me, I appreciate it. – LogicalBranch Jul 23 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.