A symlink is a soft or hard link to a directory location to another directory location or file. It effectively allows a directory tree (soft link only) to be made for different non-structured directory locations, even across partitions. It allows configuration file control by having current configuration file pointing to different version or use type configuration files, depending on use case.
Simple use is:
ln -s "path/directory or file" "path/symlink name"
, where option -s
is to create a symlink. See ln –help
or man ln
for more information.ln "path/file" "path/symlink name"
, which creates a hard link to a file. (Where: hard links to directories are not allowed and hard links to files must be on same devices)Where:
rm "path/symlink name"
ls "path/symlink name"
ls "path/symlink name/"
-f
flag to force-i
flag for interactive, it will ask to overwrite where existing link or file exists-s
flag for soft link, default is hard linkComparison Parameters | Hard link | Soft link |
---|---|---|
Inode number* | Files that are hard linked take the same inode number | Files that are soft linked take a different inode number |
Directories | Hard links are not allowed for directories* | Soft links can be used for linking directories |
File system | It cannot be used across file systems | It can be used across file systems |
Data | Data present in the original file will still be available in the hard links | Soft links only point to the file name, it does not retain data of the file |
Original file’s deletion | If the original file is removed, the link will still work as it accesses the data the original was having access to. | If the original file is removed, the link will not work as it doesn’t access the original file’s data. |
Speed | Hard links are comparatively faster | Soft links are comparatively slower |
Notes:
-d
and -F
), however most modern operating systems do not allow hard links on directories. See Why are hard links not allowed for directories?. Hard links to directories should not be used even if possible.Soft symlink ownership is not particularly important as it has full permissions (777) and file access is determined by real file permissions.
In general soft links are more flexible, however hard links on files do have specific use cases. Some examples include:
stat
command on a file/directory/symlink/device/pipe to get the statistics of the file. Remember in link all these are stored as files.find dir -xdev \! -type d -links +1 -printf '%20D %20i %p\n' | sort -n | uniq -w 42 –all-repeated=separate
will find and list files that have common inode numbers in the nominated dir