Hard links and Symbolic links YASH PAL, 3 June 202328 May 2024 Unix files consist of two parts: the data part and the filename part. The data part is called an ‘inode’. The inode carries the map of, where the data and the file permissions for the data exist. More than one filename can reference the same inode number; these files are said to be hard linked’. There’s a special file type whose data part carries a path to another file. Since it is a special file, the OS recognizes the data as a path, and redirects to reads and writes so that, instead of accessing the data within the special file, they access the data in the file named by the data in the special file. This special file is called a ‘soft link’ or a ‘symbolic link’. When a directory is built, it is initially populated with the filename parts of two special files and files. The filename part for the file is populated with the inode# of the directory file in which the entry has been made; is a hard link to the file that implements the current directory. The filename part for the ‘..’ file is populated with the inode # of the directory file that contains the filename part of the current directory file. ‘..’ is a hard link to the file that implements the immediate parent of the current directory. The ‘ln’ command knows how to build hard links and soft links; the ‘mkdir’ command knows how to build directories. There are restrictions on what can be hard-linked that are not applicable to soft links, softlinks have other restrictions not shared by hard links. ln It is used to establish more than one link for a file. $ln abc newabc This establishes one more link for the file abc in the form of the name newabc. If we take a long listing we find that these file show 2 links. ln in action [root@localhost] $ ls -liatotal 3 7347 drwxr-xr-x 12 root users 1024 May 12 21:189180 drwxr-xr-x 19 root users 2048 May 12 21:18 [root@localhost]$ echo”how r u”>name.file[root@localhost]$ ls -liatotal 47347 drwxr-xr-x 12 root users 1024 May 12 21:189180 drwxr-xr-x 19 root users 2048 May 12 21:187348 -rw-r— 1 root users 15 May 12 21:18 name.file [root@localhost]$ cat name.filehow r uNow, make a hardlink to the file[root@localhost]$ in name.file had.file[root@localhost]$ ls -liatotal 57347 drwxr-xr-x 12 root users 1024 May 12 21:189180 drwxr-xr-x 19 root users 2048 May 12 21:187348 -rw-r— 1 root users 15 May 12 21:18 name.file7348 -rw-r— 2 root users 15 May 12 21:18 hd.file [root@localhost]$ cat hd.filehow r uWe see that:(a) hd file shares the same inode (7348) as basic.file(b) hd file shares the same data as basic file. If we change the permissions on basic.file:[root@localhost]$ chmod a+w basic.file [root@localhost]$ ls -liatotal 5 7347 drwxr-xr-x 12 root users 1024 May 12 21:189180 drwxr-xr-x 19 root users 2048 May 12 21:187348 -rw-rw-rw- 1 root users 15 May 12 21:18 name.file7348 -rw-rw-rw- 2 root users 15 May 12 21:18 hd.file then the same permissions change on hardlink.file.The two files (name.file and hd.file) share the same inode and data, but have different file names.Now make a softlink to the file: [root@localhost]$ ln -s name.file sft.file[root@localhost]$ ls -liatotal 5 7347 drwxr-xr-x 12 root users 1024 May 12 21:189180 drwxr-xr-x 19 root users 2048 May 12 21:18 ..7348 -rw-rw-rw- 1 root users 15 May 12 21:18 name.file7348 -rw-rw-rw- 2 root users 15 May 12 21:18 hd.file7349 lrwxrwxrwx 1 root users 15 May 12 21:24 sft.file -> name.file [root@localhost]$ cat sft.filehow r uHere, we see that although softlink. File accesses the same data as basic.file and hardlink.file, it does not share the same inode (7349 vs 7348), nor does it exhibit the same file permissions. It shows a new permission bit: the ‘I’ (softlink) bit. If we delete name.file:[root@localhost]$ rm name.file[root@localhost]$ ls -liatotal 4 7347 drwxr-xr-x 12 root users 1024 May 12 21:189180 drwxr-xr-x 19 root users 2048 May 12 21:18 ..7348 -rw-rw-rw- 1 root users 15 May 12 21:18 hd.file7349 lrwxrwxrwx 1 root users 15 May 12 21:24 sft.file -> name.file Then we lose the ability to access the linked data through the softlink:[root@localhost]$ cat sft.filecat: softlink.file: No such file or directory But, we can access to the original data through the hardlink:[root@localhost]$ cat hd.filehow r u We will notice that when we deleted the original file, the hardlink did not vanish. Similarly, if we had deleted the softlink, the original file would not have vanished. When deleting files, the data part isn’t disposed of until all the filename parts have been deleted. There’s a count in the inode that indicates how many filenames point to this file, and that count is decremented by 1 each time one of those filenames are deleted When the count makes it to zero, the inode and its associated data are deleted. Soft or symbolic is more of a short – cut to the original file. If we delete the original file the shortcut fails and if we delete the short cut nothing happens to the original. While the hard link is a mirror copy of the original. Advantage of linking One file is to be shared in black and white with several users. This avoids duplication of the same file contents in different directories. $cp -l abc newabc This command works the same as the ln command. Soft/Symbolic Links Pointers to programs, files, or directories located elsewhere. If the original program, file, or directory is renamed, moved, or deleted then the soft link is broken. If we type ls -F we can see while files are soft links. To create a soft link called sft.file that points to a file called name.file, use $ ln -s name.file sft.file Hard Links Pointers to programs and files, but NOT directories. If the original program or file is renamed, moved, or deleted, the hard link is NOT broken. Hard links cannot span disk drives, so we CANNOT have a hard link on/dev/hdb that refers to a program or file on/dev/hda. To create a hard link called hd.file that points to a file called name file, use this $ln name.file hd.file Computer Science Tutorials Linux computer scienceLinux