Hard Links
hardlink is a feature in file systems (like those found in Unix-based operating systems such as Linux) that allows multiple filenames (or directory entries) to refer to the same physical file data on disk. When we create a hardlink to a file, we're essentially creating another name (or link) for that file's data, rather than copying the data itself.
Explanation of the Scenario:
Lets create a hardlink named shortcut-hard-link
to a file named hardlink
. Let's break down what happened and explain hardlinks based on our context:
Creating the Hardlink: we created a hardlink named shortcut-hard-link
pointing to the hardlink
file using the ln
command:
ln hardlink shortcut-hard-link
Checking the Links: Whenwe used
stat
to inspect bothhardlink
andshortcut-hard-link
, we noticed that they both have the same inode number (35320
) and both showLinks: 2
. This indicates that both filenames (hardlink
andshortcut-hard-link
) are linked to the same underlying file data.Modifying the Hardlink: After creating the hardlink, we modified the contents of the
hardlink
file by usingcat > hardlink
to write some text into it. Sinceshortcut-hard-link
is a hardlink to the same inode (and therefore the same data blocks on disk), the modifications made tohardlink
are reflected inshortcut-hard-link
.Removing the Original File (
hardlink
): When we usedrm -rf hardlink
to remove thehardlink
file,we essentially deleted one of the directory entries pointing to the underlying file data. However, sinceshortcut-hard-link
still points to the same inode (and data blocks), the data remains accessible throughshortcut-hard-link
.
Accessing the Hardlink Data: Even after deleting the hardlink
file, we were able to access the data through shortcut-hard-link
because the file data (inode) was not actually deleted until all directory entries (links) pointing to it are removed.
Summary of Hard Links:
Hardlink: In this context,
shortcut-hard-link
is a hardlink to thehardlink
file. Both filenames (hardlink
andshortcut-hard-link
) refer to the same underlying file data (inode).File Deletion: Deleting one of the hardlinks (
hardlink
) does not immediately remove the underlying file data as long as other hardlinks (likeshortcut-hard-link
) still exist.
What is a Symbolic Link (Soft Link)?
A symbolic link, also known as a symlink or soft link, is a special type of file that acts as a reference to another file or directory. Unlike hardlinks, which directly reference the inode of a file, symbolic links contain the path to the target file or directory.
Explanation of the Scenario:
Creating a Symbolic Link: we created a symbolic link named shotcut-soft
that points to softlink
using the ln -s
command:
ln -s softlink shotcut-soft
Here, shotcut-soft
is a symbolic link pointing to softlink
.
Inspecting the Files:
softlink
: This is the original file (or directory) that the symbolic linkshotcut-soft
points to.shotcut-soft
: This is the symbolic link itself, represented as a file. If we view its contents (cat shotcut-soft
), it shows the contents ofsoftlink
because it's pointing tosoftlink
.
Modifying the Original File (
softlink
):we modified the contents ofsoftlink
usingvim softlink
, adding the text "this is a demo softlink file" to it.Effect on the Symbolic Link (
shotcut-soft
): Sinceshotcut-soft
is a symbolic link tosoftlink
, any changes made tosoftlink
are immediately reflected when we viewshotcut-soft
. Both files reference the same underlying target file or directoryRemoving the Original File (
softlink
): When we deleted (rm -rf softlink
) the original file (softlink
), it was removed from the file system. As a result, attempts to accesssoftlink
directly (cat softlink
) result in a "No such file or directory" error.
Accessing the Symbolic Link (shotcut-soft
): After deleting the original file (softlink
), attempts to access the symbolic link (shotcut-soft
) also result in a "No such file or directory" error because the target of the symbolic link no longer exists.
Summary of Symbolic Links:
Reference to Target: Symbolic links are references to other files or directories by their path.
Can Cross File Systems: Symbolic links can point to files or directories across different file systems.
Affected by Target Changes: Changes to the target file or directory are reflected in symbolic links.
Deleting Target: Deleting the original file or directory (target) renders symbolic links pointing to it invalid.