Junior — Middle
Can you explain what a soft link is and how it differs from a hard link in a file system?
sobes.tech AI
Answer from AI
A soft link (or symbolic link) is a special file that contains a path to another file or directory. It acts as a pointer or shortcut to the target object. If the target file is deleted, the soft link becomes "broken" and does not work.
A hard link is an additional name for an existing file that points to the same inode in the filesystem. A hard link is fully equivalent to the original file: deleting one name does not delete the data as long as there is at least one hard link.
Main differences:
- A hard link points to an inode, a soft link points to a path.
- A soft link can link to files and directories, a hard link usually only to files (in most systems).
- A soft link can point to objects in other filesystems, a hard link only within the same filesystem.
- When the original file is deleted, the hard link continues to work, the soft link does not.
Example of creating a soft and hard link in Linux:
ln original.txt hardlink.txt # hard link
ln -s original.txt softlink.txt # soft (symbolic) link