ln

Table of Contents

1. Inodes

The inode is the administration of files, it stores metadata (access time, modification time, ownership, permissions etc) and the disk block location of files data

To access an inode you assign a name, a link (hard or symbolic) is a name, if you have multiple hard links for one inode they are just multiple names refering to the same inode

Hard links point to inodes while symbolic links point to hard links. So if the original name is removed the symbolic links becomes invalid

Use ls -li to show the files unique inode

~ $ ls -li /
  231 lrwxrwxrwx   1 root root     7 15 déc.   2021 bin -> usr/bin
      \_ means this is a symbolic link
    1 drwxr-xr-x   4 root root  4096  1 janv.  1970 boot
    1 drwxr-xr-x  15 root root  3480 25 déc.  06:01 dev
 8161 drwxr-xr-x  77 root root  4096 28 déc.  02:39 etc

2. Find hard links

~ $ ls -li
total 0
23201597 -rw-r--r-- 2 root root 0  5 janv. 21:48 bar
23201598 -rw-r--r-- 1 root root 0  5 janv. 21:48 baz
23201597 -rw-r--r-- 2 root root 0  5 janv. 21:48 foo
~ $ find . -inum 23201597
bar
foo

3. Create new symbolic link foo pointing to an existing file bar

When creating symlinks use an absolute path to avoid path issues when moving it, the symbolic link access is controlled by the target file permissions

ln -s /usr/bin/bar /usr/local/bin/foo
#     \__________/ \________________/
#        TARGET       NEW SHORTCUT

4. Create hard link foo for the file bar

ln /etc/bar /opt/foo

5. References