Hard links are a feature of file systems in Unix-like operating systems that allow multiple directory entries to refer to the same file on disk. Here are the key points about hard links:
Key Characteristics:
-
Same Inode:
- Hard links point to the same inode (the data structure that stores file metadata and location on disk). This means they share the same file content.
-
Multiple Names:
- You can create multiple hard links to a single file, each with its own name in different directories.
-
File Modification:
- Modifying the content of one hard link affects all other links because they all reference the same underlying data.
-
Deletion:
- When you delete a hard link, the file is not removed from the disk until all hard links to it are deleted. The file's data remains accessible as long as at least one hard link exists.
-
Limitations:
- Hard links cannot span different file systems (i.e., you cannot create a hard link to a file on a different mounted volume).
- You cannot create hard links for directories (to prevent circular references) except for the special entries
.(current directory) and..(parent directory).
Creating Hard Links:
To create a hard link, you can use the ln command:
ln source_file link_name
For example, if you have a file named file.txt and you want to create a hard link named link_to_file.txt, you would run:
ln file.txt link_to_file.txt
Example:
-
Create a file:
echo "Hello, World!" > file.txt -
Create a hard link:
ln file.txt link_to_file.txt -
Check inodes:
ls -liBoth
file.txtandlink_to_file.txtwill show the same inode number. -
Modify the content:
echo "New content" > file.txt -
View the content of the link:
cat link_to_file.txtThis will show "New content" because both names refer to the same file.
If you have more questions or need further clarification, feel free to ask!
