Links in Linux (Symbolic link & Hard Link)

Published: 03 June 2020
on channel: Decode ITES
519
6

#DecodeITeS

What are Links?

A link in UNIX is a pointer to any file.
Like pointers in any of the programming languages, links in UNIX are pointers that are pointing to a file or a directory.
Creating links is a kind of shortcut to access a file.
Links allow more than one file name(s) to refer to the same file, elsewhere.

Types of Links:

Soft Link or Symbolic links
Hard Links

These links behave differently when the source of a link (what is being linked to) is moved or removed.
Symbolic links are not updated like (they merely contain a string which is the pathname of its target)
Hard links always refer to the source like actual file, even if moved or removed.
For example, if we have the file a.txt. If we create a hard link to that file and then delete the file, we can still access the file using the hard link. But if we create a soft link of that file and then delete the original file, we can’t access the file through the soft link and the soft link becomes dangling in nature.
Basically, hard link increases reference count of a location file soft links work as a shortcut (like in Windows)

Hard Links
Each hard-linked file is assigned to the same Inode value as the original one, therefore they reference the same physical file location.
Hard links are more flexible and remain linked even if that original or linked files are moved throughout the file system, although hard links are unable to cross different file systems or environments.
ls -l command shows all the available links with the link column shows a number of links.
Links have the actual file contents
Removing any link just reduces the link count, but doesn’t affect the other links.
We cannot create a hard link for a directory to avoid the recursive loops.
If the original file is removed then the link still shows the content of the file.

Soft Links

A soft link is similar to the file shortcut feature which we use in Windows Operating systems.
Each soft linked file contains a separate Inode value that points to their original file.
As similar to hard links, any changes to the data in either file are reflected in the other one.
Soft links can be linked across different file systems, although if the original file is deleted or moved, the soft linked file will not work properly (called hanging link).
ls -l command shows all links with first column value l? and the link points to the original file.
Soft Link contains the path for the original file and not the contents.
Removing soft link doesn’t affect anything but removing the original file, the link becomes “dangling” link which then points to a nonexistent file.
A soft link can be linked to any directory.
The link across filesystems: If we want to link files across the filesystems, we can only use symlinks/soft links.


Watch video Links in Linux (Symbolic link & Hard Link) online without registration, duration hours minute second in high quality. This video was added by user Decode ITES 03 June 2020, don't forget to share it with your friends and acquaintances, it has been viewed on our site 519 once and liked it 6 people.