Make Links Between Files-
Objectives
Make multiple file names reference the same file with hard links and symbolic (or "soft") links.
Manage Links Between Files
You can create multiple file names that point to the same file. These file names are called links.
You can create two types of links-
1. a hard link,
2. a symbolic link (sometimes called a soft link).
Each way has its advantages and disadvantages.
Linux Inodes-
An Inode number is a uniquely existing number for all the files in Linux and all Unix type systems.
When a file is created on a system, a file name and Inode number is assigned to it.
Generally, to access a file, a user uses the file name but internally file name is first mapped with respective Inode number stored in a table.
Inode Contents
An Inode is a data structure containing metadata about the files.
Following contents are stored in the Inode from a file:
User ID of file
Group ID of file
Device ID
File size
Date of creation
Permission
Owner of the file
File protection flag
Link counter to determine number of hard links
Inode Table
The Inode table contains all the Inodes and is created when file system is created. The df -i command can be used to check how many inodes are free and left unused in the filesystem.
Create Hard Links
Every file starts with a single hard link, from its initial name to the data on the file system. When you create a hard link to a file, you create another name that points to that same data. The new hard link acts exactly like the original file name. After the link is created, you cannot tell the difference between the new hard link and the original name of the file.
Limitations of Hard Links
Hard links have some limitations. First, you can use hard links only with regular files. You cannot use the ln command to create a hard link to a directory or special file.
Second, you can use hard links only if both files are on the same file system. The file-system hierarchy can be composed of multiple storage devices. Depending on the configuration of your system, when you change into a new directory, that directory and its contents might be stored on a different file system.
Create Symbolic Links
The ln command -s option creates a symbolic link, which is also called a "soft link". A symbolic link is not a regular file, but a special type of file that points to an existing file or directory.
Symbolic links have some advantages over hard links:
• Symbolic links can link two files on different file systems.
• Symbolic links can point to a directory or special file, not just to a regular file.
In the following example, the ln -s command creates a symbolic link for the /home/user/ newfile-link2.txt file. The name for the symbolic link is /tmp/newfile-symlink.txt