Bootstrap FreeKB - Linux Commands - Create hard, soft and symbolic links (symlink) using the ln command
Linux Commands - Create hard, soft and symbolic links (symlink) using the ln command

Updated:   |  Linux Commands articles

The ln command can be used to link one file to another file. There are two types of links, a hard link and a soft link. A hard link is two files that are exact copies of each other. A soft link, also known as a symbolic link or symlink, is a reference from one file to another file.


Hard link

The ln command without any options will create a hard link. Let's take an example where there is a file named foo.txt in the /home/john.doe directory.

[john.doe@server1 ~]# ls -l /home/john.doe
-rw-rw-r--.  1  john.doe  john.doe  183  May 23 18:53  foo.txt

 

The ln command followed by the file that exists and then followed by the new file that will be created will create a hard link. In this example, a hard link is created from /home/john.doe/foo.txt to /home/jane.doe/bar.txt. bar.txt cannot exist at /home/jane.doe prior to creating the hard link.

[john.doe@server1 ~]# ln /home/john.doe/foo.txt /home/jane.doe/bar.txt

 

Every file in Linux has a unique inode number. In this example, the inode of foo.txt in the /home/john.doe directory is 33554566.

[john.doe@server1 ~]# ls -i /home/john.doe/foo.txt
33554566 foo.txt

 

Likewise, the inode of bar.txt in the /home/jane.doe directory is also 33554566. This shows that foo.txt in both the /home/john.doe and /home/jane.doe are the same exact file, and any changes to the file in either directory will change the file in both directories. For example, if john.doe adds "Hello World" to foo.txt, /home/jane.doe/bar.txt will contain Hello World. Likewise, deleting /home/john.doe/foo.txt or /home/jane.doe/bar.txt will delete the file from both directories.

[jane.doe@server1 ~]# ls -i /home/jane.doe/bar.txt
33554566 bar.txt

 


soft link / symbolic link / symlink

Almost always, symbolic links are used to create a symbolic link from a file in the /usr/bin directory to the executable file for a CLI. For example, let's say you downloaded the NodeJS CLI and extracted the CLIs to the /usr/local/nodejs/node-v20.10.0-linux-x64/bin/ on your system.

~]# ll /usr/local/nodejs/node-v20.10.0-linux-x64/bin/
-rwxr-xr-x 1 bind bind 96227728 Nov 22 05:42 node
lrwxrwxrwx 1 bind bind       38 Nov 22 05:42 npm -> ../lib/node_modules/npm/bin/npm-cli.js
lrwxrwxrwx 1 bind bind       38 Nov 22 05:42 npx -> ../lib/node_modules/npm/bin/npx-cli.js

 

And you want to be able to issue the node or npm or npx commands without having to use the full path to each command. This is a perfect example of a situation where it makes sense to create a symbolic link. The ln -s command can be used to create a symbolic link. Let's create a symbolic link that points /usr/bin/node to /usr/local/nodejs/node-v20.10.0-linux-x64/bin/node so that the node command is in our $PATH (since CLIs in the /usr/bin directory should be in our $PATH) so that we can just use the node command and not the full path to the node CLI.

ln -s /usr/local/nodejs/node-v20.10.0-linux-x64/bin/node /usr/bin/node

 

/usr/bin/node will now be symbolically linked to /usr/local/nodejs/node-v20.10.0-linux-x64/bin/node.

~]# ll /usr/bin/node
lrwxrwxrwx 1 root root 50 Mar 22 22:34 /usr/bin/node -> /usr/local/nodejs/node-v20.10.0-linux-x64/bin/node

 

Every unique file in Linux has a unique inode number.

In this example, the inode of /usr/bin/node is 30424953.

~]# ls -i /usr/bin/node
30424953 /usr/bin/node

 

And /usr/local/nodejs/node-v20.10.0-linux-x64/bin/node has a different inode, 17044703 in this example.

~]# ls -i /usr/local/nodejs/node-v20.10.0-linux-x64/bin/node
17044703 /usr/local/nodejs/node-v20.10.0-linux-x64/bin/node

 

And now the which command should return /usr/bin/node.

~]# which node
/usr/bin/node

 


Remove a link

The unlink command can be used to remove a link.




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 1a8de5 in the box below so that we can be sure you are a human.